<forward_list>
[added with C++11]
namespace std { template<class Ty, class Alloc> class forward_list; // TEMPLATE FUNCTIONS template<class Ty, class Alloc> bool operator==( const forward_list<Ty, Alloc>& lhs, const forward_list<Ty, Alloc>& rhs); template<class Ty, class Alloc> bool operator!=( const forward_list<Ty, Alloc>& lhs, const forward_list<Ty, Alloc>& rhs); template<class Ty, class Alloc> bool operator<( const forward_list<Ty, Alloc>& lhs, const forward_list<Ty, Alloc>& rhs); template<class Ty, class Alloc> bool operator>( const forward_list<Ty, Alloc>& lhs, const forward_list<Ty, Alloc>& rhs); template<class Ty, class Alloc> bool operator<=( const forward_list<Ty, Alloc>& lhs, const forward_list<Ty, Alloc>& rhs); template<class Ty, class Alloc> bool operator>=( const forward_list<Ty, Alloc>& lhs, const forward_list<Ty, Alloc>& rhs); template<class Ty, class Alloc> void swap( forward_list<Ty, Alloc>& lhs, forward_list<Ty, Alloc>& rhs); } // namespace std
Include the STL
standard header <forward_list>
to define the
container
template class forward_list
and several supporting
templates.
forward_list
allocator_type
· assign
· cbefore_begin
· before_begin
· begin
· cbegin
· cend
· clear
· const_iterator
· const_pointer
· const_reference
· difference_type
· emplace_after
· emplace_front
· empty
· end
· erase_after
· front
· get_allocator
· insert_after
· iterator
· forward_list
· max_size
· merge
· operator=
· pointer
· pop_front
· previous
· push_front
· reference
· remove
· remove_if
· resize
· reverse
· size_type
· sort
· splice_after
· swap
· unique
· value_type
template<class Ty, class Alloc = allocator<Ty> > class forward_list { public: typedef Alloc allocator_type; typedef typename Alloc::pointer pointer; typedef typename Alloc::const_pointer const_pointer; typedef typename Alloc::reference reference; typedef typename Alloc::const_reference const_reference; typedef typename Alloc::value_type value_type; typedef typename Alloc::size_type size_type; typedef typename Alloc::difference_type difference_type; typedef T0 iterator; typedef T1 const_iterator; forward_list(); explicit forward_list(const Alloc& al); explicit forward_list(size_type n); forward_list(size_type n, const Ty& val); forward_list(size_type n, const Ty& val, const Alloc& al); forward_list(const forward_list& right); forward_list(const forward_list& right, const Alloc& al); forward_list(list&& right); forward_list(list&& right, const Alloc& al); template<class InIt> forward_list(InIt first, InIt last); template<class InIt> forward_list(InIt first, InIt last, const Alloc& al); forward_list(initializer_list<Ty> init) forward_list(initializer_list<Ty> init, const Alloc& al); forward_list(list&& right); forward_list(list&& right, const Alloc& al); forward_list& operator=(const forward_list& right); forward_list& operator=(initializer_list<Ty> init) forward_list& operator=(forward_list&& right); iterator before_begin() noexcept; const_iterator before_begin() const noexcept; iterator begin(); const_iterator begin() const; iterator end(); const_iterator end() const; const_iterator cbefore_begin() noexcept; const_iterator cbegin() const; const_iterator cend() const; iterator previous(const_iterator it); const_iterator previous(const_iterator it) const; void resize(size_type n); void resize(size_type n, const Ty& x); size_type max_size() const; bool empty() const; Alloc get_allocator() const; reference front(); const_reference front() const; void push_front(const Ty& val); void push_front(Ty&& val); template<class... Valty> void emplace_front(Valty&&... val); void pop_front(); template<class InIt> void assign(InIt first, InIt last); void assign(size_type n, const Ty& x); void assign(initializer_list<Ty> init) iterator insert_after(const_iterator where, const Ty& x); iterator insert_after(const_iterator where, size_type n, const Ty& x); template<class InIt> iterator insert_after(const_iterator where, InIt first, InIt last); iterator insert_after(const iterator where, initializer_list<Ty> init) iterator insert_after(const_iterator where, Ty&& x); template<class... Valty> iterator emplace_after(const_iterator where, Valty&&... val); iterator erase_after(const_iterator where); iterator erase_after(const_iterator first, const_iterator last); void clear() noexcept; void swap(forward_list& right); void splice_after(const_iterator where, forward_list& x); void splice_after(const_iterator where, forward_list& x, iterator first); void splice_after(const_iterator where, forward_list& x, iterator first, iterator last); void splice_after(const_iterator where, forward_list&& x); void splice_after(const_iterator where, forward_list&& x, iterator first); void splice_after(const_iterator where, forward_list&& x, iterator first, iterator last); void remove(const Ty& x); template<class Pred> void remove_if(Pred pr); void unique(); template<class Pred> void unique(Pred pr); void merge(forward_list& x); template<class Pred> void merge(forward_list& x, Pred pr); void merge(forward_list&& x); template<class Pred> void merge(forward_list&& x, Pred pr); void sort(); template<class Pred> void sort(Pred pr); void reverse() noexcept; };
The template class describes an object that controls a
varying-length sequence of elements of type Ty
.
The sequence is stored as a singly linked list of elements,
each containing a member of type Ty
.
The object allocates and frees storage for the sequence it controls
through a stored allocator object
of class Alloc
. Such an allocator object must have
the same external interface as an object of template class
allocator
.
List reallocation occurs when a member function must insert, erase or splice elements of the controlled sequence. In all such cases, only iterators or references that designate erased or spliced elements of the controlled sequence become invalid.
All additions to the controlled sequence occur as if by calls to
insert_after
, which is the
only member function that calls the constructor
Ty(const Ty&)
. If such an expression throws
an exception, the container object inserts no new elements and rethrows
the exception. Thus, an object of template class forward_list
is left in a known state when such exceptions occur.
forward_list::allocator_type
typedef Alloc allocator_type;
The type is a synonym for the template parameter Alloc
.
forward_list::assign
template<class InIt> void assign(InIt first, InIt last); void assign(size_type n, const Ty& x); void assign(initializer_list<Ty> init)
If InIt
is an integer type, the first member
function behaves the same as assign((size_type)first, (Ty)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 x
.
The third member function replaces the controlled sequence
from an object of class
initializer_list<Ty>
.
forward_list::before_begin
const_iterator before_begin() const noexcept; iterator before_begin() noexcept;
The member function returns a forward iterator that points just before the first element of the sequence (or just before the end of an empty sequence).
forward_list::begin
const_iterator begin() const; iterator begin();
The member function returns a forward iterator that points at the first element of the sequence (or just beyond the end of an empty sequence).
forward_list::cbefore_begin
const_iterator cbefore_begin() const noexcept;
The member function returns a forward iterator that points just before the first element of the sequence (or just before the end of an empty sequence).
forward_list::cbegin
const_iterator cbegin() const;
The member functions return a forward iterator that points at the first element of the sequence (or just beyond the end of an empty sequence).
forward_list::cend
const_reference cend() const;
The member functions return a forward iterator that points just beyond the end of the sequence.
forward_list::clear
void clear() noexcept;
The member function calls
erase_after(
before_begin(),
end())
.
forward_list::const_iterator
typedef T1 const_iterator;
The type describes an object that can serve as a constant
forward iterator for the controlled sequence.
It is described here as a
synonym for the implementation-defined type T1
.
forward_list::const_pointer
typedef typename Alloc::const_pointer const_pointer;
The type describes an object that can serve as a constant pointer to an element of the controlled sequence.
forward_list::const_reference
typedef typename Alloc::const_reference const_reference;
The type describes an object that can serve as a constant reference to an element of the controlled sequence.
forward_list::difference_type
typedef typename Alloc::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.
forward_list::emplace_after
template<class... Valty> iterator emplace_after(const_iterator where, Valty&&... val);
The member function effectively returns
insert_after(where, value_type(forward<Valty>(val)...))
,
except that the element value is constructed in place.
forward_list::emplace_front
template<class... Valty> void emplace_front(Valty&&... val);
The member function effectively returns
insert_after(before_begin(), value_type(forward<Valty>(val)...))
,
except that the element value is constructed in place.
If an exception is thrown, the container is left unaltered and the exception is rethrown.
forward_list::empty
bool empty() const;
The member function returns true for an empty controlled sequence.
forward_list::end
const_iterator end() const; iterator end();
The member function returns a forward iterator that points just beyond the end of the sequence.
forward_list::erase_after
iterator erase_after(const_iterator where); iterator erase_after(const_iterator first, const_iterator last);
The first member function removes the element of the controlled
sequence just after where
. The second member function
removes the elements of the controlled sequence
in the range (first, last)
(neither end point is included).
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.
Reallocation occurs,
so iterators and references become
invalid for the erased
elements.
The member functions throws nothing.
forward_list::forward_list
forward_list(); explicit forward_list(const Alloc& al); explicit forward_list(size_type n); forward_list(size_type n, const Ty& val); forward_list(size_type n, const Ty& val, const Alloc& al); forward_list(const forward_list& right); forward_list(const forward_list& right, const Alloc& al); forward_list(forward_list&& right); forward_list(forward_list&& right, const Alloc& al); template<class InIt> forward_list(InIt first, InIt last); template<class InIt> forward_list(InIt first, InIt last, const Alloc& al); forward_list(initializer_list<Ty> init) forward_list(initializer_list<Ty> init, const Alloc& al);
All constructors store an
allocator object and
initialize the controlled sequence. The allocator object is the argument
al
, if present. Otherwise, for the copy and move constructors, it is
right.get_allocator()
.
Otherwise, it is Alloc()
.
The first two constructors specify an empty initial controlled sequence.
The next three constructors specify
a repetition of count
elements of value val
,
if specified. Otherwise the repeated value is Ty()
.
The next four constructors specify
a copy of the sequence controlled by right
.
The last two of these constructors are the same as the first two, but with an
rvalue reference.
The next two constructors specify the sequence
[first, last)
.
The last two constructors specify the initial controlled sequence
with an object of class
initializer_list<Ty>
.
forward_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.
forward_list::get_allocator
Alloc get_allocator() const;
The member function returns the stored allocator object.
forward_list::insert_after
iterator insert_after(const_iterator where, const Ty& x); iterator insert_after(const_iterator where, size_type n, const Ty& x); template<class InIt> iterator insert_after(const_iterator where, InIt first, InIt last); itereator insert_after(const iterator where, initializer_list<Ty> init) iterator insert_after(const_iterator where, Ty&& x);
Each of the member functions inserts, just after the element pointed to
by where
in the controlled sequence, a sequence
specified by the remaining operands, and returns an iterator that
designates the last inserted element.
The first member function inserts
a single element with value x
. The second member function
inserts a repetition of n
elements of value x
.
If InIt
is an integer type, the third member
function behaves the same as insert(it, (size_type)first, (Ty)last)
.
Otherwise, it inserts the sequence
[first, last)
, which must not overlap
the initial controlled sequence.
The fourth member function inserts the sequence
specified by an object of class
initializer_list<Ty>
.
The last member function is the same as the first, but with an rvalue reference.
Inserting N
elements causes N
constructor calls.
Reallocation occurs,
but no iterators or references become
invalid.
If an exception is thrown during the insertion of one or more elements, the container is left unaltered and the exception is rethrown.
forward_list::iterator
typedef T0 iterator;
The type describes an object that can serve as a forward
iterator for the controlled sequence.
It is described here as a
synonym for the implementation-defined type T0
.
forward_list::max_size
size_type max_size() const;
The member function returns the length of the longest sequence that the object can control.
forward_list::merge
void merge(forward_list& x); template<class Pred> void merge(forward_list& x, Pred pr); void merge(forward_list&& x); template<class Pred> void merge(forward_list&& x, Pred pr);
The first two 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.
The last two member functions are the same as the first, but with rvalue references.
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.
forward_list::operator=
forward_list& operator=(const forward_list& right); forward_list& operator=(initializer_list<Ty> init) forward_list& operator=(forward_list&& right);
The first member operator replaces the controlled sequence
with a copy of the sequence controlled by right
.
The second member operator replaces the controlled sequence
from an object of class
initializer_list<Ty>
.
The third member operator is the same as the first, but with an rvalue reference.
forward_list::pointer
typedef typename Alloc::pointer pointer;
The type describes an object that can serve as a pointer to an element of the controlled sequence.
forward_list::pop_front
void pop_front();
The member function removes the first element of the controlled sequence, which must be non-empty.
The member function never throws an exception.
forward_list::previous
iterator previous(const_iterator it); const_iterator previous(const_iterator it) const;
The member function returns an iterator that designates the element
immediately preceding it
, if possible; otherwise it returns
end()
.
This operation takes time proportional to the number of elements
in the controlled sequence (linear time complexity).
forward_list::push_front
void push_front(const Ty& val); void push_front(Ty&& val);
The member function inserts an element with value val
at the beginning of the controlled sequence.
If an exception is thrown, the container is left unaltered and the exception is rethrown.
forward_list::reference
typedef typename Alloc::reference reference;
The type describes an object that can serve as a reference to an element of the controlled sequence.
forward_list::remove
void remove(const Ty& x);
The member function removes from the controlled sequence
all elements, designated by the iterator P
, for which
*P == x
.
The member function never throws an exception.
forward_list::remove_if
template<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.
forward_list::resize
void resize(size_type n); void resize(size_type n, const Ty& x);
The member functions both ensure that the number of elements in the list
henceforth is 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 effectively call
erase_after(begin() + n - 1, end())
.
forward_list::reverse
void reverse() noexcept;
The member function reverses the order in which elements appear in the controlled sequence.
forward_list::size_type
typedef typename Alloc::size_type size_type;
The unsigned integer type describes an object that can represent the length of any controlled sequence.
forward_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 ordered pairs of elements in the original controlled sequence
are reversed in the resulting controlled sequence.
(The sort is stable.)
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.
forward_list::splice_after
void splice_after(const_iterator where, forward_list& x); void splice_after(const_iterator where, forward_list& x, iterator first); void splice_after(const_iterator where, forward_list& x, iterator first, iterator last); void splice_after(const_iterator where, forward_list&& x); void splice_after(const_iterator where, forward_list&& x, iterator first); void splice_after(const_iterator where, forward_list&& x, iterator first, iterator last);
The first member function inserts the sequence controlled
by x
just after the element in the controlled sequence
pointed to by where
. It also removes all elements from
x
. (&x
must not equal this
.)
The second member function removes the element just after
first
in the sequence controlled by x
and
inserts it just after the element in the controlled sequence
pointed to by where
. (If where == first || where == ++first
,
no change occurs.)
The third member function inserts the subrange
designated by (first, last)
from the sequence
controlled by x
just after the element in the controlled sequence pointed to by where
.
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 where
.)
If the third member function inserts
N
elements, and &x != this
, an object of class
iterator
is
incremented N
times.
For all splice_after
member functions, if
get_allocator()
== str.get_allocator()
, no exception occurs.
Otherwise, a copy and a destructor call also
occur for each inserted element.
The last three member functions are the same as the first, but with rvalue references.
No iterators or references that designate spliced elements become invalid.
The member functions throw nothing.
forward_list::swap
void swap(forward_list& right);
The member function swaps the controlled sequences between
*this
and right
. If
get_allocator()
== right.get_allocator()
, it does so in constant time,
it throws no exceptions, and it invalidates no references, pointers,
or iterators that designate elements in the two controlled sequences.
Otherwise, it performs a number of element assignments and constructor calls
proportional to the number of elements in the two controlled sequences.
forward_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)
.
For a controlled sequence of length N
(> 0), the predicate pr(*Pi, *Pj)
is evaluated N - 1
times.
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.
forward_list::value_type
typedef typename Alloc::value_type value_type;
The type is a synonym for the template parameter Ty
.
operator!=
template<class Ty, class Alloc> bool operator!=( const forward_list <Ty, Alloc>& lhs, const forward_list <Ty, Alloc>& rhs);
The template function returns !(lhs == rhs)
.
operator==
template<class Ty, class Alloc> bool operator==( const forward_list <Ty, Alloc>& lhs, const forward_list <Ty, Alloc>& rhs);
The template function overloads operator==
to compare
two objects of template class
forward_list
. The function returns
distance(
lhs.begin(),
end()) ==
distance(rhs.begin(), rhs.end()) &&
equal(lhs.
begin(), lhs.
end(), rhs.begin())
.
operator<
template<class Ty, class Alloc> bool operator<( const forward_list <Ty, Alloc>& lhs, const forward_list <Ty, Alloc>& rhs);
The template function overloads operator<
to compare
two objects of template class
forward_list
. The function returns
lexicographical_compare(lhs.
begin(), lhs.
end(), rhs.begin(), rhs.end())
.
operator<=
template<class Ty, class Alloc> bool operator<=( const forward_list <Ty, Alloc>& lhs, const forward_list <Ty, Alloc>& rhs);
The template function returns !(rhs < lhs)
.
operator>
template<class Ty, class Alloc> bool operator>( const forward_list <Ty, Alloc>& lhs, const forward_list <Ty, Alloc>& rhs);
The template function returns rhs < lhs
.
operator>=
template<class Ty, class Alloc> bool operator>=( const forward_list <Ty, Alloc>& lhs, const forward_list <Ty, Alloc>& rhs);
The template function returns !(lhs < rhs)
.
swap
template<class Ty, class Alloc> void swap( forward_list <Ty, Alloc>& lhs, forward_list <Ty, Alloc>& rhs);
The template function executes
lhs.swap(rhs)
.
See also the Table of Contents and the Index.
Copyright © 1992-2013 by P.J. Plauger. All rights reserved.