[forward.list.ops]

# 23 Containers library [[containers]](./#containers)

## 23.3 Sequence containers [[sequences]](sequences#forward.list.ops)

### 23.3.7 Class template forward_list [[forward.list]](forward.list#ops)

#### 23.3.7.6 Operations [forward.list.ops]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7580)

In this subclause,
arguments for a template parameter
named Predicate or BinaryPredicate shall meet the corresponding requirements in [[algorithms.requirements]](algorithms.requirements "26.2 Algorithms requirements")[.](#1.sentence-1)

The semantics of i + n,
where i is an iterator into the list and n is an integer,
are the same as those of next(i, n)[.](#1.sentence-2)

The expression i - n,
where i is an iterator into the list and n is an integer,
means an iterator j such that j + n == i is true[.](#1.sentence-3)

For merge and sort,
the definitions and requirements in [[alg.sorting]](alg.sorting "26.8 Sorting and related operations") apply[.](#1.sentence-4)

[ð](#lib:splice_after,forward_list)

`constexpr void splice_after(const_iterator position, forward_list& x);
constexpr void splice_after(const_iterator position, forward_list&& x);
`

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7601)

*Preconditions*: position is before_begin() or is a dereferenceable
iterator in the range [begin(), end())[.](#2.sentence-1)

get_allocator() == x.get_allocator() is true[.](#2.sentence-2)

addressof(x) != this is true[.](#2.sentence-3)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7608)

*Effects*: Inserts the contents of x afterposition, and x becomes empty[.](#3.sentence-1)

Pointers and references to the moved
elements of x now refer to those same elements but as members of *this[.](#3.sentence-2)

Iterators referring to the moved elements will continue to refer to their elements, but
they now behave as iterators into *this, not into x[.](#3.sentence-3)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7616)

*Throws*: Nothing[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7620)

*Complexity*: O(distance(x.begin(), x.end()))

[ð](#lib:splice_after,forward_list_)

`constexpr void splice_after(const_iterator position, forward_list& x, const_iterator i);
constexpr void splice_after(const_iterator position, forward_list&& x, const_iterator i);
`

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7632)

*Preconditions*: position is before_begin() or is a dereferenceable
iterator in the range [begin(), end())[.](#6.sentence-1)

The iterator following i is a dereferenceable iterator in x[.](#6.sentence-2)

get_allocator() == x.get_allocator() is true[.](#6.sentence-3)

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7639)

*Effects*: Inserts the element following i into *this, followingposition, and removes it from x[.](#7.sentence-1)

The result is unchanged if position == i or position == ++i[.](#7.sentence-2)

Pointers
and references to *++i continue to refer to the same element but as a member of*this[.](#7.sentence-3)

Iterators to *++i continue to refer to
the same element, but now behave as iterators into *this, not into x[.](#7.sentence-4)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7648)

*Throws*: Nothing[.](#8.sentence-1)

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7652)

*Complexity*: O(1)

[ð](#lib:splice_after,forward_list__)

`constexpr void splice_after(const_iterator position, forward_list& x,
                            const_iterator first, const_iterator last);
constexpr void splice_after(const_iterator position, forward_list&& x,
                            const_iterator first, const_iterator last);
`

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7666)

*Preconditions*: position is before_begin() or is a
dereferenceable iterator in the range [begin(), end())[.](#10.sentence-1)

(first, last) is a
valid range in x, and all iterators in the range (first, last) are
dereferenceable[.](#10.sentence-2)

position is not an iterator in the range (first, last)[.](#10.sentence-3)

get_allocator() == x.get_allocator() is true[.](#10.sentence-4)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7674)

*Effects*: Inserts elements in the range (first, last) after position and
removes the elements from x[.](#11.sentence-1)

Pointers and references to the moved elements ofx now refer to those same elements but as members of *this[.](#11.sentence-2)

Iterators
referring to the moved elements will continue to refer to their elements, but they now
behave as iterators into *this, not into x[.](#11.sentence-3)

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7682)

*Complexity*: O(distance(first, last))

[ð](#lib:remove,forward_list)

`constexpr size_type remove(const T& value);
template<class Predicate> constexpr size_type remove_if(Predicate pred);
`

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7695)

*Effects*: Erases all the elements in the list referred to by a list iterator i for
which the following conditions hold: *i == value (for remove()),pred(*i) is true (for remove_if())[.](#13.sentence-1)

Invalidates only the iterators and references to the erased elements[.](#13.sentence-2)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7702)

*Returns*: The number of elements erased[.](#14.sentence-1)

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7706)

*Throws*: Nothing unless an exception is thrown by the equality comparison or the
predicate[.](#15.sentence-1)

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7711)

*Complexity*: Exactly distance(begin(), end()) applications of the corresponding
predicate[.](#16.sentence-1)

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7716)

*Remarks*: [Stable](algorithm.stable "16.4.6.8 Requirements for stable algorithms [algorithm.stable]")[.](#17.sentence-1)

[ð](#lib:unique,forward_list)

`constexpr size_type unique();
template<class BinaryPredicate> constexpr size_type unique(BinaryPredicate binary_pred);
`

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7728)

Let binary_pred be equal_to<>{} for the first overload[.](#18.sentence-1)

[19](#19)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7731)

*Preconditions*: binary_pred is an equivalence relation[.](#19.sentence-1)

[20](#20)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7735)

*Effects*: Erases all but the first element from every consecutive
group of equivalent elements[.](#20.sentence-1)

That is, for a nonempty list, erases all elements referred to
by the iterator i in the range [begin() + 1, end())
for which binary_pred(*i, *(i - 1)) is true[.](#20.sentence-2)

Invalidates only the iterators and references to the erased elements[.](#20.sentence-3)

[21](#21)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7744)

*Returns*: The number of elements erased[.](#21.sentence-1)

[22](#22)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7748)

*Throws*: Nothing unless an exception is thrown by the predicate[.](#22.sentence-1)

[23](#23)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7752)

*Complexity*: If empty() is false,
exactly distance(begin(), end()) - 1 applications of
the corresponding predicate,
otherwise no applications of the predicate[.](#23.sentence-1)

[ð](#lib:merge,forward_list)

`constexpr void merge(forward_list& x);
constexpr void merge(forward_list&& x);
template<class Compare> constexpr void merge(forward_list& x, Compare comp);
template<class Compare> constexpr void merge(forward_list&& x, Compare comp);
`

[24](#24)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7769)

Let comp be less<> for the first two overloads[.](#24.sentence-1)

[25](#25)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7772)

*Preconditions*: *this and x are both sorted
with respect to the comparator comp, andget_allocator() == x.get_allocator() is true[.](#25.sentence-1)

[26](#26)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7778)

*Effects*: If addressof(x) == this, there are no effects[.](#26.sentence-1)

Otherwise, merges
the two sorted ranges [begin(), end()) and [x.begin(), x.end())[.](#26.sentence-2)

The result is a range
that is sorted with respect to the comparator comp[.](#26.sentence-3)

Pointers and references to the moved elements of x now refer to those same elements
but as members of *this[.](#26.sentence-4)

Iterators referring to the moved elements will continue to
refer to their elements, but they now behave as iterators into *this, not intox[.](#26.sentence-5)

[27](#27)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7790)

*Complexity*: At most distance(begin(),
end()) + distance(x.begin(), x.end()) - 1 comparisons
if addressof(x) != this; otherwise, no comparisons are performed[.](#27.sentence-1)

[28](#28)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7796)

*Remarks*: Stable ([[algorithm.stable]](algorithm.stable "16.4.6.8 Requirements for stable algorithms"))[.](#28.sentence-1)

If addressof(x) != this, x is empty after the merge[.](#28.sentence-2)

No elements are copied by this operation[.](#28.sentence-3)

If an exception is thrown other than by a comparison, there are no effects[.](#28.sentence-4)

[ð](#lib:sort,forward_list)

`constexpr void sort();
template<class Compare> constexpr void sort(Compare comp);
`

[29](#29)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7811)

*Effects*: Sorts the list according to the operator< or the comp function object[.](#29.sentence-1)

If an exception is thrown, the order of the elements in *this is unspecified[.](#29.sentence-2)

Does not affect the validity of iterators and references[.](#29.sentence-3)

[30](#30)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7817)

*Complexity*: Approximately NlogN comparisons, where N is distance(begin(), end())[.](#30.sentence-1)

[31](#31)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7821)

*Remarks*: [Stable](algorithm.stable "16.4.6.8 Requirements for stable algorithms [algorithm.stable]")[.](#31.sentence-1)

[ð](#lib:reverse,forward_list)

`constexpr void reverse() noexcept;
`

[32](#32)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7832)

*Effects*: Reverses the order of the elements in the list[.](#32.sentence-1)

Does not affect the validity of iterators and references[.](#32.sentence-2)

[33](#33)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L7837)

*Complexity*: Linear time[.](#33.sentence-1)
