📄 stl_list.h
字号:
* This function will insert copies of the data in the range [@a
* first,@a last) into the %list before the location specified by
* @a position.
*
* Due to the nature of a %list this operation can be done in
* constant time, and does not invalidate iterators and
* references.
*/
template<typename _InputIterator>
void
insert(iterator __position, _InputIterator __first,
_InputIterator __last)
{
// Check whether it's an integral type. If so, it's not an iterator.
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_insert_dispatch(__position, __first, __last, _Integral());
}
/**
* @brief Remove element at given position.
* @param position Iterator pointing to element to be erased.
* @return An iterator pointing to the next element (or end()).
*
* This function will erase the element at the given position and thus
* shorten the %list by one.
*
* Due to the nature of a %list this operation can be done in
* constant time, and only invalidates iterators/references to
* the element being removed. The user is also cautioned that
* this function only erases the element, and that if the element
* is itself a pointer, the pointed-to memory is not touched in
* any way. Managing the pointer is the user's responsibilty.
*/
iterator
erase(iterator __position);
/**
* @brief Remove a range of elements.
* @param first Iterator pointing to the first element to be erased.
* @param last Iterator pointing to one past the last element to be
* erased.
* @return An iterator pointing to the element pointed to by @a last
* prior to erasing (or end()).
*
* This function will erase the elements in the range @a
* [first,last) and shorten the %list accordingly.
*
* Due to the nature of a %list this operation can be done in
* constant time, and only invalidates iterators/references to
* the element being removed. The user is also cautioned that
* this function only erases the elements, and that if the
* elements themselves are pointers, the pointed-to memory is not
* touched in any way. Managing the pointer is the user's
* responsibilty.
*/
iterator
erase(iterator __first, iterator __last)
{
while (__first != __last)
__first = erase(__first);
return __last;
}
/**
* @brief Swaps data with another %list.
* @param x A %list of the same element and allocator types.
*
* This exchanges the elements between two lists in constant
* time. Note that the global std::swap() function is
* specialized such that std::swap(l1,l2) will feed to this
* function.
*/
void
swap(list& __x)
{ _List_node_base::swap(this->_M_impl._M_node,__x._M_impl._M_node); }
/**
* Erases all the elements. Note that this function only erases
* the elements, and that if the elements themselves are
* pointers, the pointed-to memory is not touched in any way.
* Managing the pointer is the user's responsibilty.
*/
void
clear()
{
_Base::_M_clear();
_Base::_M_init();
}
// [23.2.2.4] list operations
/**
* @brief Insert contents of another %list.
* @param position Iterator referencing the element to insert before.
* @param x Source list.
*
* The elements of @a x are inserted in constant time in front of
* the element referenced by @a position. @a x becomes an empty
* list.
*/
void
splice(iterator __position, list& __x)
{
if (!__x.empty())
this->_M_transfer(__position, __x.begin(), __x.end());
}
/**
* @brief Insert element from another %list.
* @param position Iterator referencing the element to insert before.
* @param x Source list.
* @param i Iterator referencing the element to move.
*
* Removes the element in list @a x referenced by @a i and
* inserts it into the current list before @a position.
*/
void
splice(iterator __position, list&, iterator __i)
{
iterator __j = __i;
++__j;
if (__position == __i || __position == __j)
return;
this->_M_transfer(__position, __i, __j);
}
/**
* @brief Insert range from another %list.
* @param position Iterator referencing the element to insert before.
* @param x Source list.
* @param first Iterator referencing the start of range in x.
* @param last Iterator referencing the end of range in x.
*
* Removes elements in the range [first,last) and inserts them
* before @a position in constant time.
*
* Undefined if @a position is in [first,last).
*/
void
splice(iterator __position, list&, iterator __first, iterator __last)
{
if (__first != __last)
this->_M_transfer(__position, __first, __last);
}
/**
* @brief Remove all elements equal to value.
* @param value The value to remove.
*
* Removes every element in the list equal to @a value.
* Remaining elements stay in list order. Note that this
* function only erases the elements, and that if the elements
* themselves are pointers, the pointed-to memory is not
* touched in any way. Managing the pointer is the user's
* responsibilty.
*/
void
remove(const _Tp& __value);
/**
* @brief Remove all elements satisfying a predicate.
* @param Predicate Unary predicate function or object.
*
* Removes every element in the list for which the predicate
* returns true. Remaining elements stay in list order. Note
* that this function only erases the elements, and that if the
* elements themselves are pointers, the pointed-to memory is
* not touched in any way. Managing the pointer is the user's
* responsibilty.
*/
template<typename _Predicate>
void
remove_if(_Predicate);
/**
* @brief Remove consecutive duplicate elements.
*
* For each consecutive set of elements with the same value,
* remove all but the first one. Remaining elements stay in
* list order. Note that this function only erases the
* elements, and that if the elements themselves are pointers,
* the pointed-to memory is not touched in any way. Managing
* the pointer is the user's responsibilty.
*/
void
unique();
/**
* @brief Remove consecutive elements satisfying a predicate.
* @param BinaryPredicate Binary predicate function or object.
*
* For each consecutive set of elements [first,last) that
* satisfy predicate(first,i) where i is an iterator in
* [first,last), remove all but the first one. Remaining
* elements stay in list order. Note that this function only
* erases the elements, and that if the elements themselves are
* pointers, the pointed-to memory is not touched in any way.
* Managing the pointer is the user's responsibilty.
*/
template<typename _BinaryPredicate>
void
unique(_BinaryPredicate);
/**
* @brief Merge sorted lists.
* @param x Sorted list to merge.
*
* Assumes that both @a x and this list are sorted according to
* operator<(). Merges elements of @a x into this list in
* sorted order, leaving @a x empty when complete. Elements in
* this list precede elements in @a x that are equal.
*/
void
merge(list& __x);
/**
* @brief Merge sorted lists according to comparison function.
* @param x Sorted list to merge.
* @param StrictWeakOrdering Comparison function definining
* sort order.
*
* Assumes that both @a x and this list are sorted according to
* StrictWeakOrdering. Merges elements of @a x into this list
* in sorted order, leaving @a x empty when complete. Elements
* in this list precede elements in @a x that are equivalent
* according to StrictWeakOrdering().
*/
template<typename _StrictWeakOrdering>
void
merge(list&, _StrictWeakOrdering);
/**
* @brief Reverse the elements in list.
*
* Reverse the order of elements in the list in linear time.
*/
void
reverse()
{ this->_M_impl._M_node.reverse(); }
/**
* @brief Sort the elements.
*
* Sorts the elements of this list in NlogN time. Equivalent
* elements remain in list order.
*/
void
sort();
/**
* @brief Sort the elements according to comparison function.
*
* Sorts the elements of this list in NlogN time. Equivalent
* elements remain in list order.
*/
template<typename _StrictWeakOrdering>
void
sort(_StrictWeakOrdering);
protected:
// Internal assign functions follow.
// Called by the range assign to implement [23.1.1]/9
template<typename _Integer>
void
_M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
{
_M_fill_assign(static_cast<size_type>(__n),
static_cast<value_type>(__val));
}
// Called by the range assign to implement [23.1.1]/9
template<typename _InputIterator>
void
_M_assign_dispatch(_InputIterator __first, _InputIterator __last,
__false_type);
// Called by assign(n,t), and the range assign when it turns out
// to be the same thing.
void
_M_fill_assign(size_type __n, const value_type& __val);
// Internal insert functions follow.
// Called by the range insert to implement [23.1.1]/9
template<typename _Integer>
void
_M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
__true_type)
{
_M_fill_insert(__pos, static_cast<size_type>(__n),
static_cast<value_type>(__x));
}
// Called by the range insert to implement [23.1.1]/9
template<typename _InputIterator>
void
_M_insert_dispatch(iterator __pos,
_InputIterator __first, _InputIterator __last,
__false_type)
{
for ( ; __first != __last; ++__first)
_M_insert(__pos, *__first);
}
// Called by insert(p,n,x), and the range insert when it turns out
// to be the same thing.
void
_M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
{
for ( ; __n > 0; --__n)
_M_insert(__pos, __x);
}
// Moves the elements from [first,last) before position.
void
_M_transfer(iterator __position, iterator __first, iterator __last)
{ __position._M_node->transfer(__first._M_node,__last._M_node); }
// Inserts new element at position given and with value given.
void
_M_insert(iterator __position, const value_type& __x)
{
_Node* __tmp = _M_create_node(__x);
__tmp->hook(__position._M_node);
}
// Erases element at position given.
void
_M_erase(iterator __position)
{
__position._M_node->unhook();
_Node* __n = static_cast<_Node*>(__position._M_node);
std::_Destroy(&__n->_M_data);
_M_put_node(__n);
}
};
/**
* @brief List equality comparison.
* @param x A %list.
* @param y A %list of the same type as @a x.
* @return True iff the size and elements of the lists are equal.
*
* This is an equivalence relation. It is linear in the size of
* the lists. Lists are considered equivalent if their sizes are
* equal, and if corresponding elements compare equal.
*/
template<typename _Tp, typename _Alloc>
inline bool
operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
{
typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;
const_iterator __end1 = __x.end();
const_iterator __end2 = __y.end();
const_iterator __i1 = __x.begin();
const_iterator __i2 = __y.begin();
while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
{
++__i1;
++__i2;
}
return __i1 == __end1 && __i2 == __end2;
}
/**
* @brief List ordering relation.
* @param x A %list.
* @param y A %list of the same type as @a x.
* @return True iff @a x is lexicographically less than @a y.
*
* This is a total ordering relation. It is linear in the size of the
* lists. The elements must be comparable with @c <.
*
* See std::lexicographical_compare() for how the determination is made.
*/
template<typename _Tp, typename _Alloc>
inline bool
operator<(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
{ return std::lexicographical_compare(__x.begin(), __x.end(),
__y.begin(), __y.end()); }
/// Based on operator==
template<typename _Tp, typename _Alloc>
inline bool
operator!=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
{ return !(__x == __y); }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator>(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
{ return __y < __x; }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator<=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
{ return !(__y < __x); }
/// Based on operator<
template<typename _Tp, typename _Alloc>
inline bool
operator>=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
{ return !(__x < __y); }
/// See std::list::swap().
template<typename _Tp, typename _Alloc>
inline void
swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
{ __x.swap(__y); }
} // namespace std
#endif /* _LIST_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -