# <slist>
<slist>
# ``
* * *
namespace std {
template
class slist;
// TEMPLATE FUNCTIONS
template
bool operator==(
const slist& lhs,
const slist& rhs);
template
bool operator!=(
const slist& lhs,
const slist& rhs);
template
bool operator<(
const slist& lhs,
const slist& rhs);
template
bool operator>(
const slist& lhs,
const slist& rhs);
template
bool operator<=(
const slist& lhs,
const slist& rhs);
template
bool operator>=(
const slist& lhs,
const slist& rhs);
template
void swap(
slist& lhs,
slist& rhs);
} // namespace stdCopy to clipboard
Include the [STL](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#STL)
standard header **``** to define the
[container](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_lib_cont.html#Containers)
template class `slist` and several supporting
templates.
## `operator!=`
template
bool operator!=(
const slist & lhs,
const slist & rhs);Copy to clipboard
The template function returns `!(lhs == rhs)`.
## `operator==`
template
bool operator==(
const slist & lhs,
const slist & rhs);Copy to clipboard
The template function overloads `operator==` to compare
two objects of template class
[`slist`](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_slist.html#slist). The function returns
`lhs.size() == rhs.size() &&
equal(lhs.
begin(), lhs.
end(), rhs.begin())`.
## `operator<`
template
bool operator<(
const slist & lhs,
const slist & rhs);Copy to clipboard
The template function overloads `operator<` to compare
two objects of template class
[`slist`](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_slist.html#slist). The function returns
`lexicographical_compare(lhs.
begin(), lhs.
end(), rhs.begin(), rhs.end())`.
## `operator<=`
template
bool operator<=(
const slist & lhs,
const slist & rhs);Copy to clipboard
The template function returns `!(rhs < lhs)`.
## `operator>`
template
bool operator>(
const slist & lhs,
const slist & rhs);Copy to clipboard
The template function returns `rhs < lhs`.
## `operator>=`
template
bool operator>=(
const slist & lhs,
const slist & rhs);Copy to clipboard
The template function returns `!(lhs < rhs)`.
## `slist`
* * *
**`allocator_type· assign· back· begin· clear· const_iterator· const_pointer· const_reference· difference_type· empty· end· erase· front· get_allocator· insert· iterator· slist· max_size· merge· pointer· pop_back· pop_front· previous· push_back· push_front· reference· remove· remove_if· resize· reverse· size· size_type· sort· splice· swap· unique· value_type`**
* * *
template >
class slist {
public:
typedef A allocator_type;
typedef typename A::pointer pointer;
typedef typename A::const_pointer
const_pointer;
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 T2 size_type;
typedef T3 difference_type;
slist();
explicit slist(const A& al);
explicit slist(size_type n);
slist(size_type n, const T& v);
slist(size_type n, const T& v, const A& al);
slist(const slist& x);
template
slist(InIt first, InIt last);
template
slist(InIt first, InIt last, const A& al);
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
iterator previous(const_iterator it);
const_iterator previous(const_iterator it) 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
void assign(InIt first, InIt last);
void assign(size_type n, const T& x);
iterator insert(iterator it, const T& x);
void insert(iterator it, size_type n, const T& x);
template
void insert(iterator it, InIt first, InIt last);
iterator erase(iterator it);
iterator erase(iterator first, iterator last);
void clear();
void swap(slist& x);
void splice(iterator it, slist& x);
void splice(iterator it, slist& x, iterator first);
void splice(iterator it, slist& x, iterator first,
iterator last);
void remove(const T& x);
templace
void remove_if(Pred pr);
void unique();
template
void unique(Pred pr);
void merge(slist& x);
template
void merge(slist& x, Pred pr);
void sort();
template
void sort(Pred pr);
void reverse();
};Copy to clipboard
The template class describes an object that controls a
varying-length sequence of elements of type `T`.
The sequence is stored as a singly linked list of elements,
each containing a member of type `T`.
The object allocates and frees storage for the sequence it controls
through a stored [allocator object](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_memory.html#allocator%20object)
of class `A`. Such an allocator object must have
the same external interface as an object of template class
[`allocator`](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_memory.html#allocator).
Note that the stored allocator object is *not* copied when the container
object is assigned.
**List reallocation**
occurs when a member function must insert, erase or splice elements of
the controlled sequence. In all such cases, only the following iterators
or references become
**invalid**:
- iterators that designated a position
**immediately beyond** an inserted element
- iterators that designate an erased element or a position
**immediately beyond** an erased element
- iterators that designate a spliced element or a position
**immediately beyond** a spliced element
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 `slist`
is left in a known state when such exceptions occur.
### `slist::allocator_type`
typedef A allocator_type;Copy to clipboard
The type is a synonym for the template parameter `A`.
### `slist::assign`
template
void assign(InIt first, InIt last);
void assign(size_type n, const T& x);Copy to clipboard
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 `x`.
### `slist::back`
reference back();
const_reference back() const;Copy to clipboard
The member function returns a reference to the last element of the
controlled sequence, which must be non-empty.
### `slist::begin`
const_iterator begin() const;
iterator begin();Copy to clipboard
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).
### `slist::clear`
void clear();Copy to clipboard
The member function calls
`erase(
begin(),
end())`.
### `slist::const_iterator`
typedef T1 const_iterator;Copy to clipboard
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`.
### `slist::const_pointer`
typedef typename A::const_pointer
const_pointer;Copy to clipboard
The type describes an object that can serve as a constant pointer
to an element of the controlled sequence.
### `slist::const_reference`
typedef typename A::const_reference const_reference;Copy to clipboard
The type describes an object that can serve as a constant reference
to an element of the controlled sequence.
### `slist::difference_type`
typedef T3 difference_type;Copy to clipboard
The signed integer type describes an object that can represent the
difference between the addresses of any two elements in the controlled
sequence. It is described here as a
synonym for the implementation-defined type `T3`.
### `slist::empty`
bool empty() const;Copy to clipboard
The member function returns true for an empty controlled sequence.
### `slist::end`
const_iterator end() const;
iterator end();Copy to clipboard
The member function returns a forward iterator that points
just beyond the end of the sequence.
### `slist::erase`
iterator erase(iterator it);
iterator erase(iterator first, iterator last);Copy to clipboard
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.
[Reallocation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_slist.html#slist%20reallocation) occurs,
so iterators and references become
[invalid](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_slist.html#invalid%20slist%20iterators) for the erased
elements and iterators become invalid for any remaining element
immediately beyond an erased element.
The member functions never throw an exception.
### `slist::front`
reference front();
const_reference front() const;Copy to clipboard
The member function returns a reference to the first element of the
controlled sequence, which must be non-empty.
### `slist::get_allocator`
A get_allocator() const;Copy to clipboard
The member function returns the stored
[allocator object](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_memory.html#allocator%20object).
### `slist::insert`
iterator insert(iterator it, const T& x);
void insert(iterator it, size_type n, const T& x);
template
void insert(iterator it, InIt first, InIt last);Copy to clipboard
Each of the member functions inserts, before the element pointed to
by `it` in the controlled sequence, a sequence
specified by the remaining operands.
The first member function inserts
a single element with value `x` and returns an iterator
that designates the newly inserted element. The second 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.
[Reallocation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_slist.html#slist%20reallocation) occurs,
so iterators become
[invalid for any element
that was immediately beyond `it`](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_slist.html#invalid%20slist%20iterators).
If an exception is thrown during the
insertion of one or more elements, the container is left unaltered
and the exception is rethrown.
### `slist::iterator`
typedef T0 iterator;Copy to clipboard
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`.
### `slist::max_size`
size_type max_size() const;Copy to clipboard
The member function returns the length of the longest sequence that
the object can control.
### `slist::merge`
void merge(slist& x);
template
void merge(slist& x, Pred pr);Copy to clipboard
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](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_lib_stl.html#sequence%20ordering) 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.
### `slist::pointer`
typedef typename A::pointer pointer;Copy to clipboard
The type describes an object that can serve as a pointer to an
element of the controlled sequence.
### `slist::pop_back`
void pop_back();Copy to clipboard
The member function removes the last element of the
controlled sequence, which must be non-empty.
This operation takes time proportional to the number of elements
in the controlled sequence (linear time complexity).
The member function never throws an exception.
### `slist::pop_front`
void pop_front();Copy to clipboard
The member function removes the first element of the
controlled sequence, which must be non-empty.
The member function never throws an exception.
### `slist::previous`
iterator previous(const_iterator it);
const_iterator previous(const_iterator it) const;Copy to clipboard
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).
### `slist::push_back`
void push_back(const T& x);Copy to clipboard
The member function inserts an element with value `x`
at the end of the controlled sequence.
If an exception is thrown, the container is left unaltered
and the exception is rethrown.
### `slist::push_front`
void push_front(const T& x);Copy to clipboard
The member function inserts an element with value `x`
at the beginning of the controlled sequence.
If an exception is thrown, the container is left unaltered
and the exception is rethrown.
### `slist::reference`
typedef typename A::reference reference;Copy to clipboard
The type describes an object that can serve as a reference to an
element of the controlled sequence.
### `slist::remove`
void remove(const T& x);Copy to clipboard
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.
### `slist::remove_if`
templace
void remove_if(Pred pr);Copy to clipboard
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.
### `slist::resize`
void resize(size_type n);
void resize(size_type n, T x);Copy to clipboard
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())`.
### `slist::reverse`
void reverse();Copy to clipboard
The member function reverses the order in which elements appear
in the controlled sequence.
### `slist::size`
size_type size() const;Copy to clipboard
The member function returns the length of the controlled sequence.
### `slist::size_type`
typedef T2 size_type;Copy to clipboard
The unsigned integer type describes an object that can represent the
length of any controlled sequence. It is described here as a
synonym for the implementation-defined type `T2`.
### `slist::slist`
slist();
explicit slist(const A& al);
explicit slist(size_type n);
slist(size_type n, const T& v);
slist(size_type n, const T& v,
const A& al);
slist(const slist& x);
template
slist(InIt first, InIt last);
template
slist(InIt first, InIt last, const A& al);Copy to clipboard
All constructors store an
[allocator object](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_memory.html#allocator%20object) 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)`.
### `slist::sort`
void sort();
template
void sort(Pred pr);Copy to clipboard
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.
### `slist::splice`
void splice(iterator it, slist& x);
void splice(iterator it, slist& x, iterator first);
void splice(iterator it, slist& x, iterator first,
iterator last);Copy to clipboard
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
`get_allocator()
== str.get_allocator()`, no exception occurs.
Otherwise, a copy and a destructor call also
occur for each inserted element.
Iterators or references that designate
spliced elements, or that designate the first element beyond a
sequence of spliced elements, become
**[invalid](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_slist.html#invalid%20slist%20iterators)**.
### `slist::swap`
void swap(slist& x);Copy to clipboard
The member function swaps the controlled sequences between
`*this` and `x`. If
`get_allocator()
== x.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.
### `slist::unique`
void unique();
template
void unique(Pred pr);Copy to clipboard
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.
### `slist::value_type`
typedef typename A::value_type value_type;Copy to clipboard
The type is a synonym for the template parameter `T`.
## `swap`
template
void swap(
slist & lhs,
slist & rhs);Copy to clipboard
The template function executes
`lhs.swap(rhs)`.
* * *
See also the
**[Table of Contents](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#Table%20of%20Contents)** and the
**[Index](https://docs.qualcomm.com/doc/80-N3040-23/topic/_cpp_index.html)**.
*[Copyright](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_crit_pjp.html) © 1992-2006by P.J. Plauger. All rights reserved.*
Last Published: Oct 06, 2026
Previous Topic
Next Topic