<list>
namespace std { template<class T, class A> class list; // TEMPLATE FUNCTIONS template<class T, class A> bool operator==( const list<T, A>& lhs, const list<T, A>& rhs); template<class T, class A> bool operator!=( const list<T, A>& lhs, const list<T, A>& rhs); template<class T, class A> bool operator<( const list<T, A>& lhs, const list<T, A>& rhs); template<class T, class A> bool operator>( const list<T, A>& lhs, const list<T, A>& rhs); template<class T, class A> bool operator<=( const list<T, A>& lhs, const list<T, A>& rhs); template<class T, class A> bool operator>=( const list<T, A>& lhs, const list<T, A>& rhs); template<class T, class A> void swap( const list<T, A>& lhs, const list<T, A>& rhs); };
Include the STL
standard header <list>
to define the
container
template class list
and three supporting
templates.
list
allocator_type
· assign
· back
· begin
· clear
· const_iterator
· const_reference
· const_reverse_iterator
· difference_type
· empty
· end
· erase
· front
· get_allocator
· insert
· iterator
· list
· max_size
· merge
· pop_back
· pop_front
· push_back
· push_front
· rbegin
· reference
· remove
· remove_if
· rend
· resize
· reverse
· reverse_iterator
· size
· size_type
· sort
· splice
· swap
· unique
· value_type
template<class T, class A = allocator<T> > class list { public: typedef A allocator_type; typedef typename A::size_type size_type; typedef typename A::difference_type difference_type; typedef typename A::reference reference; typedef typename A::const_reference const_reference; typedef typename A::value_type value_type; typedef T0 iterator; typedef T1 const_iterator; typedef reverse_iterator<const_iterator> const_reverse_iterator; typedef reverse_iterator<iterator> reverse_iterator; list(); explicit list(const A& al); explicit list(size_type n); list(size_type n, const T& v); list(size_type n, const T& v, const A& al); list(const list& x); template<class InIt> list(InIt first, InIt last); template<class InIt> list(InIt first, InIt last, const A& al); iterator begin(); const_iterator begin() const; iterator end(); iterator end() const; reverse_iterator rbegin(); const_reverse_iterator rbegin() const; reverse_iterator rend(); const_reverse_iterator rend() const; void resize(size_type n); void resize(size_type n, T x); size_type size() const; size_type max_size() const; bool empty() const; A get_allocator() const; reference front(); const_reference front() const; reference back(); const_reference back() const; void push_front(const T& x); void pop_front(); void push_back(const T& x); void pop_back(); template<class InIt> void assign(InIt first, InIt last); void assign(size_type n); void assign(size_type n, const T& x); iterator insert(iterator it); iterator insert(iterator it, const T& x); void insert(iterator it, size_type n, const T& x); template<class InIt> void insert(iterator it, InIt first, InIt last); iterator erase(iterator it); iterator erase(iterator first, iterator last); void clear(); void swap(list x); void splice(iterator it, list& x); void splice(iterator it, list& x, iterator first); void splice(iterator it, list& x, iterator first, iterator last); void remove(const T& x); templace<class Pred> void remove_if(Pred pr); void unique(); template<class Pred> void unique(Pred pr); void merge(list& x); template<class Pred> void merge(list& x, Pred pr); void sort(); template<class Pred> void sort(Pred pr); void reverse(); protected: A allocator; };
The template class describes an object that controls a
varying-length sequence of elements of type T
.
The sequence is stored as a bidirectional linked list of elements,
each containing a member of type T
.
The object allocates and frees storage for the sequence it controls
through a protected object named
allocator
,
of class A
. Such an
allocator object must have
the same external interface as an object of template class
allocator
.
Note that allocator
is not copied when the
object is assigned.
List reallocation occurs when a member function must insert or erase elements of the controlled sequence. In all such cases, only iterators or references that point at erased portions of the controlled sequence become invalid.
All additions to the controlled sequence occur as if by calls to
insert
, which is the
only member function that calls the constructor
T(const T&)
. If such an expression throws
an exception, the container object inserts no new elements and rethrows
the exception. Thus, an object of template class list
is left in a known state when such exceptions occur.
list::allocator_type
typedef A allocator_type;
The type is a synonym for the template parameter A
.
list::assign
template<class InIt> void assign(InIt first, InIt last); void assign(size_type n); void assign(size_type n, const T& x);
If InIt
is an integer type, the first member
function behaves the same as assign((size_type)first, (T)last)
.
Otherwise, the
first member function replaces the sequence
controlled by *this
with the sequence
[first, last)
, which must not overlap
the initial controlled sequence.
The second member function replaces the sequence
controlled by *this
with a repetition of n
elements of value
T2()
.
The second member function replaces the sequence
controlled by *this
with a repetition of n
elements of value x
.
list::back
reference back(); const_reference back() const;
The member function returns a reference to the last element of the controlled sequence, which must be non-empty.
list::begin
const_iterator begin() const; iterator begin();
The member function returns a bidirectional iterator that points at the first element of the sequence (or just beyond the end of an empty sequence).
list::clear
void clear();
The member function calls
erase(
begin(),
end())
.
list::const_iterator
typedef T1 const_iterator;
The type describes an object that can serve as a constant
bidirectional iterator for the controlled sequence.
It is described here as a
synonym for the unspecified type T1
.
list::const_reference
typedef typename A::const_reference const_reference;
The type describes an object that can serve as a constant reference to an element of the controlled sequence.
list::const_reverse_iterator
typedef reverse_iterator<const_iterator> const_reverse_iterator;
The type describes an object that can serve as a constant reverse bidirectional iterator for the controlled sequence.
list::difference_type
typedef typename A::difference_type difference_type;
The signed integer type describes an object that can represent the difference between the addresses of any two elements in the controlled sequence.
list::empty
bool empty() const;
The member function returns true for an empty controlled sequence.
list::end
const_iterator end() const; iterator end();
The member function returns a bidirectional iterator that points just beyond the end of the sequence.
list::erase
iterator erase(iterator it); iterator erase(iterator first, iterator last);
The first member function removes the element of the controlled
sequence pointed to by it
. The second member function
removes the elements of the controlled sequence
in the range [first, last)
.
Both return an iterator that designates the first element remaining
beyond any elements removed, or
end()
if no such element exists.
Erasing N
elements causes
N
destructor calls. No
reallocation occurs,
so iterators and references become
invalid only for the erased
elements.
list::front
reference front(); const_reference front() const;
The member function returns a reference to the first element of the controlled sequence, which must be non-empty.
list::get_allocator
A get_allocator() const;
The member function returns
allocator
.
list::insert
iterator insert(iterator it); iterator insert(iterator it, const T& x); void insert(iterator it, size_type n, const T& x); template<class InIt> void insert(iterator it, InIt first, InIt last);
Each of the member functions inserts, before the element pointed to
by it
in the controlled sequence, a sequence
specified by the remaining operands.
If the constructor T(const T&)
or T()
throws an exception,
the member function leaves the controlled sequence unchanged and
rethrows the exception.
The first member function inserts
a single element with value T()
and returns an iterator
that points to the newly inserted element. The second member function inserts
a single element with value x
and returns an iterator
that points to the newly inserted element. The third member function
inserts a repetition of n
elements of value x
.
If InIt
is an integer type, the last member
function behaves the same as insert(it, (size_type)first, (T)last)
.
Otherwise, the last member function inserts the sequence
[first, last)
, which must not overlap
the initial controlled sequence.
Inserting N
elements causes N
constructor calls. No
reallocation occurs,
so no iterators or references become
invalid.
list::iterator
typedef T0 iterator;
The type describes an object that can serve as a bidirectional
iterator for the controlled sequence.
It is described here as a
synonym for the unspecified type T0
.
list::list
list(); explicit list(const A& al); explicit list(size_type n); list(size_type n, const T& v); list(size_type n, const T& v, const A& al); list(const list& x); template<class InIt> list(InIt first, InIt last); template<class InIt> list(InIt first, InIt last, const A& al);
All constructors store an
allocator object in
allocator
and
initialize the controlled sequence. The allocator object is the argument
al
, if present. For the copy constructor, it is
x.get_allocator()
.
Otherwise, it is A()
.
The first two constructors specify an
empty initial controlled sequence. The third constructor specifies
a repetition of n
elements of value T()
.
The fourth and fifth constructors specify
a repetition of n
elements of value x
.
The sixth constructor specifies a copy of the sequence controlled by
x
.
If InIt
is an integer type, the last two constructors
specify a repetition of (size_type)first
elements of value
(T)last
. Otherwise, the
last two constructors specify the sequence
[first, last)
. None of the constructors perform any interim
reallocations.
list::max_size
size_type max_size() const;
The member function returns the length of the longest sequence that the object can control.
list::merge
void merge(list& x); template<class Pred> void merge(list& x, Pred pr);
Both member functions remove all elements from the sequence
controlled by x
and insert them in the controlled
sequence. Both sequences must be ordered by the same predicate,
described below. The resulting sequence is also ordered by that
predicate.
For the iterators Pi
and Pj
designating elements at positions i
and j
, the first member function imposes the
order !(*Pj < *Pi)
whenever i < j
.
(The elements are sorted in ascending order.)
The second member function imposes the order
!pr(*Pj, *Pi)
whenever i < j
.
No pairs of elements in the original controlled sequence
are reversed in the resulting controlled sequence. If a pair
of elements in the resulting controlled sequence compares equal
(!(*Pi < *Pj) && !(*Pj < *Pi)
),
an element from the original controlled sequence appears before
an element from the sequence controlled by x
.
An exception occurs only if pr
throws an exception.
In that case, the controlled sequence is left in unspecified order
and the exception is rethrown.
list::pop_back
void pop_back();
The member function removes the last element of the controlled sequence, which must be non-empty.
list::push_back
void push_back(const T& x);
The member function inserts an element with value x
at the end of the controlled sequence.
list::pop_front
void pop_front();
The member function removes the first element of the controlled sequence, which must be non-empty.
list::push_front
void push_front(const T& x);
The member function inserts an element with value x
at the beginning of the controlled sequence.
list::rbegin
const_reverse_iterator rbegin() const; reverse_iterator rbegin();
The member function returns a reverse bidirectional iterator that points just beyond the end of the controlled sequence. Hence, it designates the beginning of the reverse sequence.
list::reference
typedef typename A::reference reference;
The type describes an object that can serve as a reference to an element of the controlled sequence.
list::remove
void remove(const T& x);
The member function removes from the controlled sequence
all elements, designated by the iterator P
, for which
*P == x
.
list::remove_if
templace<class Pred> void remove_if(Pred pr);
The member function removes from the controlled sequence
all elements, designated by the iterator P
, for which
pr(*P)
is true.
An exception occurs only if pr
throws an exception.
In that case, the controlled sequence is left in an unspecified state
and the exception is rethrown.
list::rend
const_reverse_iterator rend() const; reverse_iterator rend();
The member function returns a reverse bidirectional iterator that points at the first element of the sequence (or just beyond the end of an empty sequence). Hence, it designates the end of the reverse sequence.
list::resize
void resize(size_type n); void resize(size_type n, T x);
The member functions both ensure that
size()
henceforth
returns n
. If it must make the controlled sequence longer,
the first member function
appends elements with value T()
, while the second member function
appends elements with value x
.
To make the controlled sequence shorter, both member functions call
erase(begin() + n, end())
.
list::reverse
void reverse();
The member function reverses the order in which elements appear in the controlled sequence.
list::reverse_iterator
typedef reverse_iterator<iterator> reverse_iterator;
The type describes an object that can serve as a reverse bidirectional iterator for the controlled sequence.
list::size
size_type size() const;
The member function returns the length of the controlled sequence.
list::size_type
typedef typename A::size_type size_type;
The unsigned integer type describes an object that can represent the length of any controlled sequence.
list::sort
void sort(); template<class Pred> void sort(Pred pr);
Both member functions order the elements in the controlled sequence by a predicate, described below.
For the iterators Pi
and Pj
designating elements at positions i
and j
, the first member function imposes the
order !(*Pj < *Pi)
whenever i < j
.
(The elements are sorted in ascending order.)
The member template function imposes the order
!pr(*Pj, *Pi)
whenever i < j
.
No pairs of elements in the original controlled sequence
are reversed in the resulting controlled sequence.
An exception occurs only if pr
throws an exception.
In that case, the controlled sequence is left in unspecified order
and the exception is rethrown.
list::splice
void splice(iterator it, list& x); void splice(iterator it, list& x, iterator first); void splice(iterator it, list& x, iterator first, iterator last);
The first member function inserts the sequence controlled
by x
before the element in the controlled sequence
pointed to by it
. It also removes all elements from
x
. (&x
must not equal this
.)
The second member function removes the element pointed to by
first
in the sequence controlled by x
and
inserts it before the element in the controlled sequence
pointed to by it
. (If it == first || it == ++first
,
no change occurs.)
The third member function inserts the subrange
designated by [first, last)
from the sequence
controlled by x
before the element in the controlled sequence pointed to by it
.
It also removes the original subrange from the sequence controlled
by x
. (If &x == this
,
the range [first, last)
must not include the element
pointed to by it
.)
If the third member function inserts
N
elements, and &x != this
, an object of class
iterator
is
incremented N
times.
For all splice
member functions, If
allocator
== str.allocator
, no exception occurs.
Otherwise, a copy and a destructor call also
occur for each inserted element.
In all cases, only iterators or references that point at spliced elements become invalid.
list::swap
void swap(list& str);
The member function swaps the controlled sequences between
*this
and str
. If
allocator
== str.allocator
, it does so in constant time;
and it throws no exceptions. Otherwise,
it performs a number of element assignments and constructor calls
proportional to the number of elements in the two controlled sequences.
list::unique
void unique(); template<class Pred> void unique(Pred pr);
The first member function removes from the controlled sequence
every element that compares equal to its preceding element.
For the iterators Pi
and Pj
designating elements at positions i
and j
, the second member function removes every
element for which i + 1 == j && pr(*Pi, *Pj)
.
An exception occurs only if pr
throws an exception.
In that case, the controlled sequence is left in an unspecified state
and the exception is rethrown.
For a controlled sequence of length N
(> 0), the predicate pr(*Pi, *Pj)
is evaluated N - 1
times.
list::value_type
typedef typename A::value_type value_type;
The type is a synonym for the template parameter T
.
operator!=
template<class T, class A> bool operator!=( const list <T, A>& lhs, const list <T, A>& rhs);
The template function returns !(lhs == rhs)
.
operator==
template<class T, class A> bool operator==( const list <T, A>& lhs, const list <T, A>& rhs);
The template function overloads operator==
to compare
two objects of template class
list
. The function returns
lhs.size() == rhs.size() &&
equal(lhs.
begin(), lhs.
end(), rhs.begin())
.
operator<
template<class T, class A> bool operator<( const list <T, A>& lhs, const list <T, A>& rhs);
The template function overloads operator<
to compare
two objects of template class
list
. The function returns
lexicographical_compare(lhs.
begin(), lhs.
end(), rhs.begin(), rhs.end())
.
operator<=
template<class T, class A> bool operator<=( const list <T, A>& lhs, const list <T, A>& rhs);
The template function returns !(rhs < lhs)
.
operator>
template<class T, class A> bool operator>( const list <T, A>& lhs, const list <T, A>& rhs);
The template function returns rhs < lhs
.
operator>=
template<class T, class A> bool operator>=( const list <T, A>& lhs, const list <T, A>& rhs);
The template function returns !(rhs < lhs)
.
swap
template<class T, class A> void swap( const list <T, A>& lhs, const list <T, A>& rhs);
The template function executes
lhs.swap(rhs)
.
See also the Table of Contents and the Index.
Copyright © 1992-1996 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.