[sequences] (original) (raw)

22 Containers library [containers]

22.3 Sequence containers [sequences]

22.3.1 In general [sequences.general]

The headers<array> ([array.syn]),<deque> ([deque.syn]),<forward_­list> ([forward.list.syn]),<list> ([list.syn]), and<vector> ([vector.syn]) define class templates that meet the requirements for sequence containers.

The following exposition-only alias template may appear in deduction guides for sequence containers:

template using iter-value-type = typename iterator_traits::value_type;

22.3.2 Header synopsis [array.syn]

#include
#include

namespace std {

template<class T, size_t N> struct array;

template<class T, size_t N> constexpr bool operator==(const array<T, N>& x, const array<T, N>& y); template<class T, size_t N> constexpr synth-three-way-result operator<=>(const array<T, N>& x, const array<T, N>& y);

template<class T, size_t N> constexpr void swap(array<T, N>& x, array<T, N>& y) noexcept(noexcept(x.swap(y)));

template<class T, size_t N> constexpr array<remove_cv_t, N> to_array(T (&a)[N]); template<class T, size_t N> constexpr array<remove_cv_t, N> to_array(T (&&a)[N]);

template struct tuple_size; template<size_t I, class T> struct tuple_element; template<class T, size_t N> struct tuple_size<array<T, N>>; template<size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>; template<size_t I, class T, size_t N> constexpr T& get(array<T, N>&) noexcept; template<size_t I, class T, size_t N> constexpr T&& get(array<T, N>&&) noexcept; template<size_t I, class T, size_t N> constexpr const T& get(const array<T, N>&) noexcept; template<size_t I, class T, size_t N> constexpr const T&& get(const array<T, N>&&) noexcept; }

22.3.3 Header synopsis [deque.syn]

#include
#include

namespace std {

template<class T, class Allocator = allocator> class deque;

template<class T, class Allocator> bool operator==(const deque<T, Allocator>& x, const deque<T, Allocator>& y); template<class T, class Allocator> synth-three-way-result operator<=>(const deque<T, Allocator>& x, const deque<T, Allocator>& y);

template<class T, class Allocator> void swap(deque<T, Allocator>& x, deque<T, Allocator>& y) noexcept(noexcept(x.swap(y)));

template<class T, class Allocator, class U> typename deque<T, Allocator>::size_type erase(deque<T, Allocator>& c, const U& value); template<class T, class Allocator, class Predicate> typename deque<T, Allocator>::size_type erase_if(deque<T, Allocator>& c, Predicate pred);

namespace pmr { template using deque = std::deque<T, polymorphic_allocator>; } }

22.3.4 Header <forward_­list> synopsis [forward.list.syn]

#include
#include

namespace std {

template<class T, class Allocator = allocator> class forward_list;

template<class T, class Allocator> bool operator==(const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y); template<class T, class Allocator> synth-three-way-result operator<=>(const forward_list<T, Allocator>& x, const forward_list<T, Allocator>& y);

template<class T, class Allocator> void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y) noexcept(noexcept(x.swap(y)));

template<class T, class Allocator, class U> typename forward_list<T, Allocator>::size_type erase(forward_list<T, Allocator>& c, const U& value); template<class T, class Allocator, class Predicate> typename forward_list<T, Allocator>::size_type erase_if(forward_list<T, Allocator>& c, Predicate pred);

namespace pmr { template using forward_list = std::forward_list<T, polymorphic_allocator>; } }

22.3.5 Header synopsis [list.syn]

#include
#include

namespace std {

template<class T, class Allocator = allocator> class list;

template<class T, class Allocator> bool operator==(const list<T, Allocator>& x, const list<T, Allocator>& y); template<class T, class Allocator> synth-three-way-result operator<=>(const list<T, Allocator>& x, const list<T, Allocator>& y);

template<class T, class Allocator> void swap(list<T, Allocator>& x, list<T, Allocator>& y) noexcept(noexcept(x.swap(y)));

template<class T, class Allocator, class U> typename list<T, Allocator>::size_type erase(list<T, Allocator>& c, const U& value); template<class T, class Allocator, class Predicate> typename list<T, Allocator>::size_type erase_if(list<T, Allocator>& c, Predicate pred);

namespace pmr { template using list = std::list<T, polymorphic_allocator>; } }

22.3.6 Header synopsis [vector.syn]

#include
#include

namespace std {

template<class T, class Allocator = allocator> class vector;

template<class T, class Allocator> constexpr bool operator==(const vector<T, Allocator>& x, const vector<T, Allocator>& y); template<class T, class Allocator> constexpr synth-three-way-result operator<=>(const vector<T, Allocator>& x, const vector<T, Allocator>& y);

template<class T, class Allocator> constexpr void swap(vector<T, Allocator>& x, vector<T, Allocator>& y) noexcept(noexcept(x.swap(y)));

template<class T, class Allocator, class U> constexpr typename vector<T, Allocator>::size_type erase(vector<T, Allocator>& c, const U& value); template<class T, class Allocator, class Predicate> constexpr typename vector<T, Allocator>::size_type erase_if(vector<T, Allocator>& c, Predicate pred);

template class vector<bool, Allocator>;

template struct hash; template struct hash<vector<bool, Allocator>>;

namespace pmr { template using vector = std::vector<T, polymorphic_allocator>; } }

22.3.7 Class template array [array]

22.3.7.1 Overview [array.overview]

The header <array> defines a class template for storing fixed-size sequences of objects.

An instance of array<T, N> stores N elements of type T, so that size() == N is an invariant.

An array is an aggregate that can be list-initialized with up to N elements whose types are convertible to T.

An array meets all of the requirements of a container and of a reversible container ([container.requirements]), except that a default constructed array object is not empty and that swap does not have constant complexity.

Descriptions are provided here only for operations on array that are not described in one of these tables and for operations where there is additional semantic information.

array<T, N> is a structural type ([temp.param]) ifT is a structural type.

Two values a1 and a2 of type array<T, N>are template-argument-equivalent ([temp.type]) if and only if each pair of corresponding elements in a1 and a2are template-argument-equivalent.

namespace std { template<class T, size_t N> struct array {

using value_type             = T;
using pointer                = T*;
using const_pointer          = const T*;
using reference              = T&;
using const_reference        = const T&;
using size_type              = size_t;
using difference_type        = ptrdiff_t;
using iterator               = implementation-defined; 
using const_iterator         = implementation-defined; 
using reverse_iterator       = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;


constexpr void fill(const T& u);
constexpr void swap(array&) noexcept(is_nothrow_swappable_v<T>);


constexpr iterator               begin() noexcept;
constexpr const_iterator         begin() const noexcept;
constexpr iterator               end() noexcept;
constexpr const_iterator         end() const noexcept;

constexpr reverse_iterator       rbegin() noexcept;
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr reverse_iterator       rend() noexcept;
constexpr const_reverse_iterator rend() const noexcept;

constexpr const_iterator         cbegin() const noexcept;
constexpr const_iterator         cend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;


[[nodiscard]] constexpr bool empty() const noexcept;
constexpr size_type size() const noexcept;
constexpr size_type max_size() const noexcept;


constexpr reference       operator[](size_type n);
constexpr const_reference operator[](size_type n) const;
constexpr reference       at(size_type n);
constexpr const_reference at(size_type n) const;
constexpr reference       front();
constexpr const_reference front() const;
constexpr reference       back();
constexpr const_reference back() const;

constexpr T *       data() noexcept;
constexpr const T * data() const noexcept;

};

template<class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>; }

22.3.7.2 Constructors, copy, and assignment [array.cons]

The conditions for an aggregate shall be met.

In addition to the requirements specified in the container requirements table, the implicit move constructor and move assignment operator for arrayrequire that T be Cpp17MoveConstructible or Cpp17MoveAssignable, respectively.

template<class T, class... U> array(T, U...) -> array<T, 1 + sizeof...(U)>;

Mandates: (is_­same_­v<T, U> && ...) is true.

22.3.7.3 Member functions [array.members]

constexpr size_type size() const noexcept;

constexpr T* data() noexcept;constexpr const T* data() const noexcept;

Returns:A pointer such that [data(), data() + size()) is a valid range.

For a non-empty array, data() == addressof(front()).

constexpr void fill(const T& u);

Effects:As if by fill_­n(begin(), N, u).

constexpr void swap(array& y) noexcept(is_nothrow_swappable_v<T>);

Effects:Equivalent to swap_­ranges(begin(), end(), y.begin()).

[ Note

:

Unlike the swap function for other containers, array​::​swaptakes linear time, may exit via an exception, and does not cause iterators to become associated with the other container.

end note

]

22.3.7.4 Specialized algorithms [array.special]

template<class T, size_t N> constexpr void swap(array<T, N>& x, array<T, N>& y) noexcept(noexcept(x.swap(y)));

Constraints: N == 0 or is_­swappable_­v<T> is true.

Effects:As if by x.swap(y).

22.3.7.5 Zero-sized arrays [array.zero]

array shall provide support for the special case N == 0.

In the case that N == 0, begin() == end() == unique value.

The return value of data() is unspecified.

The effect of calling front() or back() for a zero-sized array is undefined.

Member function swap() shall have a non-throwing exception specification.

22.3.7.6 Array creation functions [array.creation]

template<class T, size_t N> constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]);

Mandates: is_­array_­v<T> is false andis_­constructible_­v<T, T&> is true.

Preconditions: T meets the Cpp17CopyConstructible requirements.

Returns: {{ a[0], …, a[N - 1] }}.

template<class T, size_t N> constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]);

Mandates: is_­array_­v<T> is false andis_­move_­constructible_­v<T> is true.

Preconditions: T meets the Cpp17MoveConstructible requirements.

Returns: {{ std​::​move(a[0]), …, std​::​move(a[N - 1]) }}.

22.3.7.7 Tuple interface [array.tuple]

template<class T, size_t N> struct tuple_size<array<T, N>> : integral_constant<size_t, N> { };

template<size_t I, class T, size_t N> struct tuple_element<I, array<T, N>> { using type = T;};

template<size_t I, class T, size_t N> constexpr T& get(array<T, N>& a) noexcept;template<size_t I, class T, size_t N> constexpr T&& get(array<T, N>&& a) noexcept;template<size_t I, class T, size_t N> constexpr const T& get(const array<T, N>& a) noexcept;template<size_t I, class T, size_t N> constexpr const T&& get(const array<T, N>&& a) noexcept;

Returns:A reference to the element of a, where indexing is zero-based.

22.3.8 Class template deque [deque]

22.3.8.1 Overview [deque.overview]

In addition, it supports constant time insert and erase operations at the beginning or the end; insert and erase in the middle take linear time.

That is, a deque is especially optimized for pushing and popping elements at the beginning and end.

Storage management is handled automatically.

Adequemeets all of the requirements of a container, of a reversible container (given in tables in [container.requirements]), of a sequence container, including the optional sequence container requirements ([sequence.reqmts]), and of an allocator-aware container (Table 76).

Descriptions are provided here only for operations ondequethat are not described in one of these tables or for operations where there is additional semantic information.

namespace std { template<class T, class Allocator = allocator> class deque { public:

using value_type             = T;
using allocator_type         = Allocator;
using pointer                = typename allocator_traits<Allocator>::pointer;
using const_pointer          = typename allocator_traits<Allocator>::const_pointer;
using reference              = value_type&;
using const_reference        = const value_type&;
using size_type              = implementation-defined; 
using difference_type        = implementation-defined; 
using iterator               = implementation-defined; 
using const_iterator         = implementation-defined; 
using reverse_iterator       = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;


deque() : deque(Allocator()) { }
explicit deque(const Allocator&);
explicit deque(size_type n, const Allocator& = Allocator());
deque(size_type n, const T& value, const Allocator& = Allocator());
template<class InputIterator>
  deque(InputIterator first, InputIterator last, const Allocator& = Allocator());
deque(const deque& x);
deque(deque&&);
deque(const deque&, const Allocator&);
deque(deque&&, const Allocator&);
deque(initializer_list<T>, const Allocator& = Allocator());

~deque();
deque& operator=(const deque& x);
deque& operator=(deque&& x)
  noexcept(allocator_traits<Allocator>::is_always_equal::value);
deque& operator=(initializer_list<T>);
template<class InputIterator>
  void assign(InputIterator first, InputIterator last);
void assign(size_type n, const T& t);
void assign(initializer_list<T>);
allocator_type get_allocator() const noexcept;


iterator               begin() noexcept;
const_iterator         begin() const noexcept;
iterator               end() noexcept;
const_iterator         end() const noexcept;
reverse_iterator       rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
reverse_iterator       rend() noexcept;
const_reverse_iterator rend() const noexcept;

const_iterator         cbegin() const noexcept;
const_iterator         cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;


[[nodiscard]] bool empty() const noexcept;
size_type size() const noexcept;
size_type max_size() const noexcept;
void      resize(size_type sz);
void      resize(size_type sz, const T& c);
void      shrink_to_fit();


reference       operator[](size_type n);
const_reference operator[](size_type n) const;
reference       at(size_type n);
const_reference at(size_type n) const;
reference       front();
const_reference front() const;
reference       back();
const_reference back() const;


template<class... Args> reference emplace_front(Args&&... args);
template<class... Args> reference emplace_back(Args&&... args);
template<class... Args> iterator emplace(const_iterator position, Args&&... args);

void push_front(const T& x);
void push_front(T&& x);
void push_back(const T& x);
void push_back(T&& x);

iterator insert(const_iterator position, const T& x);
iterator insert(const_iterator position, T&& x);
iterator insert(const_iterator position, size_type n, const T& x);
template<class InputIterator>
  iterator insert(const_iterator position, InputIterator first, InputIterator last);
iterator insert(const_iterator position, initializer_list<T>);

void pop_front();
void pop_back();

iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
void     swap(deque&)
  noexcept(allocator_traits<Allocator>::is_always_equal::value);
void     clear() noexcept;

};

template<class InputIterator, class Allocator = allocator<iter-value-type>> deque(InputIterator, InputIterator, Allocator = Allocator()) -> deque<iter-value-type, Allocator>;

template<class T, class Allocator> void swap(deque<T, Allocator>& x, deque<T, Allocator>& y) noexcept(noexcept(x.swap(y))); }

22.3.8.2 Constructors, copy, and assignment [deque.cons]

explicit deque(const Allocator&);

Effects:Constructs an emptydeque, using the specified allocator.

explicit deque(size_type n, const Allocator& = Allocator());

Effects:Constructs a deque withn default-inserted elements using the specified allocator.

Preconditions: T is Cpp17DefaultInsertable into *this.

deque(size_type n, const T& value, const Allocator& = Allocator());

Effects:Constructs adequewith n copies of value, using the specified allocator.

Preconditions: T is Cpp17CopyInsertable into *this.

template<class InputIterator> deque(InputIterator first, InputIterator last, const Allocator& = Allocator());

Effects:Constructs adequeequal to the range[first, last), using the specified allocator.

Complexity:Linear in distance(first, last).

22.3.8.3 Capacity [deque.capacity]

void resize(size_type sz);

Preconditions: T is Cpp17MoveInsertable and Cpp17DefaultInsertable into *this.

Effects:If sz < size(), erases the last size() - sz elements from the sequence.

Otherwise, appends sz - size() default-inserted elements to the sequence.

void resize(size_type sz, const T& c);

Preconditions: T is Cpp17CopyInsertable into *this.

Effects:If sz < size(), erases the last size() - sz elements from the sequence.

Otherwise, appends sz - size() copies of c to the sequence.

Preconditions: T is Cpp17MoveInsertable into *this.

Effects: shrink_­to_­fit is a non-binding request to reduce memory use but does not change the size of the sequence.

[ Note

:

The request is non-binding to allow latitude for implementation-specific optimizations.

end note

]

If the size is equal to the old capacity, or if an exception is thrown other than by the move constructor of a non-Cpp17CopyInsertable T, then there are no effects.

Complexity:If the size is not equal to the old capacity, linear in the size of the sequence; otherwise constant.

Remarks:If the size is not equal to the old capacity, then invalidates all the references, pointers, and iterators referring to the elements in the sequence, as well as the past-the-end iterator.

22.3.8.4 Modifiers [deque.modifiers]

iterator insert(const_iterator position, const T& x); iterator insert(const_iterator position, T&& x); iterator insert(const_iterator position, size_type n, const T& x);template<class InputIterator> iterator insert(const_iterator position, InputIterator first, InputIterator last); iterator insert(const_iterator position, initializer_list<T>);template<class... Args> reference emplace_front(Args&&... args);template<class... Args> reference emplace_back(Args&&... args);template<class... Args> iterator emplace(const_iterator position, Args&&... args);void push_front(const T& x);void push_front(T&& x);void push_back(const T& x);void push_back(T&& x);

Effects:An insertion in the middle of the deque invalidates all the iterators and references to elements of the deque.

An insertion at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.

Remarks:If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator ofTthere are no effects.

If an exception is thrown while inserting a single element at either end, there are no effects.

Otherwise, if an exception is thrown by the move constructor of a non-Cpp17CopyInsertable T, the effects are unspecified.

Complexity:The complexity is linear in the number of elements inserted plus the lesser of the distances to the beginning and end of the deque.

Inserting a single element at either the beginning or end of a deque always takes constant time and causes a single call to a constructor ofT.

iterator erase(const_iterator position); iterator erase(const_iterator first, const_iterator last);void pop_front();void pop_back();

Effects:An erase operation that erases the last element of a deque invalidates only the past-the-end iterator and all iterators and references to the erased elements.

An erase operation that erases the first element of a deque but not the last element invalidates only iterators and references to the erased elements.

An erase operation that erases neither the first element nor the last element of a deque invalidates the past-the-end iterator and all iterators and references to all the elements of the deque.

[ Note

:

pop_­front and pop_­back are erase operations.

end note

]

Complexity:The number of calls to the destructor of T is the same as the number of elements erased, but the number of calls to the assignment operator of T is no more than the lesser of the number of elements before the erased elements and the number of elements after the erased elements.

Throws:Nothing unless an exception is thrown by the assignment operator ofT.

22.3.8.5 Erasure [deque.erasure]

template<class T, class Allocator, class U> typename deque<T, Allocator>::size_type erase(deque<T, Allocator>& c, const U& value);

Effects:Equivalent to:

auto it = remove(c.begin(), c.end(), value); auto r = distance(it, c.end()); c.erase(it, c.end()); return r;

template<class T, class Allocator, class Predicate> typename deque<T, Allocator>::size_type erase_if(deque<T, Allocator>& c, Predicate pred);

Effects:Equivalent to:

auto it = remove_if(c.begin(), c.end(), pred); auto r = distance(it, c.end()); c.erase(it, c.end()); return r;

22.3.9 Class template forward_­list [forwardlist]

22.3.9.1 Overview [forwardlist.overview]

A forward_­list is a container that supports forward iterators and allows constant time insert and erase operations anywhere within the sequence, with storage management handled automatically.

Fast random access to list elements is not supported.

[ Note

:

It is intended that forward_­list have zero space or time overhead relative to a hand-written C-style singly linked list.

Features that would conflict with that goal have been omitted.

end note

]

A forward_­list meets all of the requirements of a container (Table 73), except that the size()member function is not provided and operator== has linear complexity.

A forward_­list also meets all of the requirements for an allocator-aware container (Table 76).

In addition, a forward_­listprovides the assign member functions (Table 77) and several of the optional container requirements (Table 78).

Descriptions are provided here only for operations onforward_­list that are not described in that table or for operations where there is additional semantic information.

[ Note

:

Modifying any list requires access to the element preceding the first element of interest, but in a forward_­list there is no constant-time way to access a preceding element.

For this reason, ranges that are modified, such as those supplied toerase and splice, must be open at the beginning.

end note

]

namespace std { template<class T, class Allocator = allocator> class forward_list { public:

using value_type      = T;
using allocator_type  = Allocator;
using pointer         = typename allocator_traits<Allocator>::pointer;
using const_pointer   = typename allocator_traits<Allocator>::const_pointer;
using reference       = value_type&;
using const_reference = const value_type&;
using size_type       = implementation-defined; 
using difference_type = implementation-defined; 
using iterator        = implementation-defined; 
using const_iterator  = implementation-defined; 


forward_list() : forward_list(Allocator()) { }
explicit forward_list(const Allocator&);
explicit forward_list(size_type n, const Allocator& = Allocator());
forward_list(size_type n, const T& value, const Allocator& = Allocator());
template<class InputIterator>
  forward_list(InputIterator first, InputIterator last, const Allocator& = Allocator());
forward_list(const forward_list& x);
forward_list(forward_list&& x);
forward_list(const forward_list& x, const Allocator&);
forward_list(forward_list&& x, const Allocator&);
forward_list(initializer_list<T>, const Allocator& = Allocator());
~forward_list();
forward_list& operator=(const forward_list& x);
forward_list& operator=(forward_list&& x)
  noexcept(allocator_traits<Allocator>::is_always_equal::value);
forward_list& operator=(initializer_list<T>);
template<class InputIterator>
  void assign(InputIterator first, InputIterator last);
void assign(size_type n, const T& t);
void assign(initializer_list<T>);
allocator_type get_allocator() const noexcept;


iterator before_begin() noexcept;
const_iterator before_begin() const noexcept;
iterator begin() noexcept;
const_iterator begin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;

const_iterator cbegin() const noexcept;
const_iterator cbefore_begin() const noexcept;
const_iterator cend() const noexcept;


[[nodiscard]] bool empty() const noexcept;
size_type max_size() const noexcept;


reference front();
const_reference front() const;


template<class... Args> reference emplace_front(Args&&... args);
void push_front(const T& x);
void push_front(T&& x);
void pop_front();

template<class... Args> iterator emplace_after(const_iterator position, Args&&... args);
iterator insert_after(const_iterator position, const T& x);
iterator insert_after(const_iterator position, T&& x);

iterator insert_after(const_iterator position, size_type n, const T& x);
template<class InputIterator>
  iterator insert_after(const_iterator position, InputIterator first, InputIterator last);
iterator insert_after(const_iterator position, initializer_list<T> il);

iterator erase_after(const_iterator position);
iterator erase_after(const_iterator position, const_iterator last);
void swap(forward_list&)
  noexcept(allocator_traits<Allocator>::is_always_equal::value);

void resize(size_type sz);
void resize(size_type sz, const value_type& c);
void clear() noexcept;


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

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

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

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

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

void reverse() noexcept;

};

template<class InputIterator, class Allocator = allocator<iter-value-type>> forward_list(InputIterator, InputIterator, Allocator = Allocator()) -> forward_list<iter-value-type, Allocator>;

template<class T, class Allocator> void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y) noexcept(noexcept(x.swap(y))); }

T shall be complete before any member of the resulting specialization of forward_­list is referenced.

22.3.9.2 Constructors, copy, and assignment [forwardlist.cons]

explicit forward_list(const Allocator&);

Effects:Constructs an empty forward_­list object using the specified allocator.

explicit forward_list(size_type n, const Allocator& = Allocator());

Preconditions: T is Cpp17DefaultInsertable into *this.

Effects:Constructs a forward_­list object with ndefault-inserted elements using the specified allocator.

forward_list(size_type n, const T& value, const Allocator& = Allocator());

Preconditions: T is Cpp17CopyInsertable into *this.

Effects:Constructs a forward_­list object with n copies of value using the specified allocator.

template<class InputIterator> forward_list(InputIterator first, InputIterator last, const Allocator& = Allocator());

Effects:Constructs a forward_­list object equal to the range [first, last).

Complexity:Linear in distance(first, last).

22.3.9.3 Iterators [forwardlist.iter]

iterator before_begin() noexcept; const_iterator before_begin() const noexcept; const_iterator cbefore_begin() const noexcept;

Returns:A non-dereferenceable iterator that, when incremented, is equal to the iterator returned by begin().

Effects: cbefore_­begin() is equivalent toconst_­cast<forward_­list const&>(*this).before_­begin().

Remarks: before_­begin() == end() shall equal false.

22.3.9.5 Modifiers [forwardlist.modifiers]

None of the overloads of insert_­after shall affect the validity of iterators and references, and erase_­after shall invalidate only iterators and references to the erased elements.

If an exception is thrown during insert_­after there shall be no effect.

Inserting n elements into a forward_­list is linear inn, and the number of calls to the copy or move constructor of T is exactly equal to n.

Erasing n elements from a forward_­list is linear in n and the number of calls to the destructor of type T is exactly equal to n.

template<class... Args> reference emplace_front(Args&&... args);

Effects:Inserts an object of type value_­type constructed withvalue_­type(std​::​forward<Args>(​args)...) at the beginning of the list.

void push_front(const T& x);void push_front(T&& x);

Effects:Inserts a copy of x at the beginning of the list.

Effects:As if by erase_­after(before_­begin()).

iterator insert_after(const_iterator position, const T& x); iterator insert_after(const_iterator position, T&& x);

Preconditions: position is before_­begin() or is a dereferenceable iterator in the range [begin(), end()).

Effects:Inserts a copy of x after position.

Returns:An iterator pointing to the copy of x.

iterator insert_after(const_iterator position, size_type n, const T& x);

Preconditions: position is before_­begin() or is a dereferenceable iterator in the range [begin(), end()).

Effects:Inserts n copies of x after position.

Returns:An iterator pointing to the last inserted copy of x or position if n == 0.

template<class InputIterator> iterator insert_after(const_iterator position, InputIterator first, InputIterator last);

Preconditions: position is before_­begin() or is a dereferenceable iterator in the range [begin(), end()).

Neither first nor last are iterators in *this.

Effects:Inserts copies of elements in [first, last) after position.

Returns:An iterator pointing to the last inserted element or position if first == last.

iterator insert_after(const_iterator position, initializer_list<T> il);

Effects: insert_­after(p, il.begin(), il.end()).

Returns:An iterator pointing to the last inserted element or position if il is empty.

template<class... Args> iterator emplace_after(const_iterator position, Args&&... args);

Preconditions: position is before_­begin() or is a dereferenceable iterator in the range [begin(), end()).

Effects:Inserts an object of type value_­type constructed withvalue_­type(std​::​forward<Args>(​args)...) after position.

Returns:An iterator pointing to the new object.

iterator erase_after(const_iterator position);

Preconditions:The iterator following position is dereferenceable.

Effects:Erases the element pointed to by the iterator following position.

Returns:An iterator pointing to the element following the one that was erased, or end() if no such element exists.

iterator erase_after(const_iterator position, const_iterator last);

Preconditions:All iterators in the range (position, last) are dereferenceable.

Effects:Erases the elements in the range (position, last).

void resize(size_type sz);

Preconditions: T is Cpp17DefaultInsertable into *this.

Effects:If sz < distance(begin(), end()), erases the last distance(begin(), end()) - sz elements from the list.

Otherwise, inserts sz - distance(begin(), end()) default-inserted elements at the end of the list.

void resize(size_type sz, const value_type& c);

Preconditions: T is Cpp17CopyInsertable into *this.

Effects:If sz < distance(begin(), end()), erases the last distance(begin(), end()) - sz elements from the list.

Otherwise, inserts sz - distance(begin(), end())copies of c at the end of the list.

Effects:Erases all elements in the range [begin(), end()).

Remarks:Does not invalidate past-the-end iterators.

22.3.9.6 Operations [forwardlist.ops]

In this subclause, arguments for a template parameter named Predicate or BinaryPredicateshall meet the corresponding requirements in [algorithms.requirements].

For merge and sort, the definitions and requirements in [alg.sorting] apply.

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

Preconditions: position is before_­begin() or is a dereferenceable iterator in the range [begin(), end()).

get_­allocator() == x.get_­allocator() is true.

addressof(x) != this is true.

Effects:Inserts the contents of x afterposition, and x becomes empty.

Pointers and references to the moved elements of x now refer to those same elements but as members of *this.

Iterators referring to the moved elements will continue to refer to their elements, but they now behave as iterators into *this, not into x.

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

Preconditions: position is before_­begin() or is a dereferenceable iterator in the range [begin(), end()).

The iterator following i is a dereferenceable iterator in x.

get_­allocator() == x.get_­allocator() is true.

Effects:Inserts the element following i into *this, followingposition, and removes it from x.

The result is unchanged if position == i or position == ++i.

Pointers and references to *++i continue to refer to the same element but as a member of*this.

Iterators to *++i continue to refer to the same element, but now behave as iterators into *this, not into x.

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

Preconditions: position is before_­begin() or is a dereferenceable iterator in the range [begin(), end()).

(first, last) is a valid range in x, and all iterators in the range (first, last) are dereferenceable.

position is not an iterator in the range (first, last).

get_­allocator() == x.get_­allocator() is true.

Effects:Inserts elements in the range (first, last) after position and removes the elements from x.

Pointers and references to the moved elements ofx now refer to those same elements but as members of *this.

Iterators referring to the moved elements will continue to refer to their elements, but they now behave as iterators into *this, not into x.

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

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()).

Invalidates only the iterators and references to the erased elements.

Returns:The number of elements erased.

Throws:Nothing unless an exception is thrown by the equality comparison or the predicate.

Complexity:Exactly distance(begin(), end()) applications of the corresponding predicate.

size_type unique();template<class BinaryPredicate> size_type unique(BinaryPredicate pred);

Effects:Erases all but the first element from every consecutive group of equal elements referred to by the iterator i in the range [first + 1, last) for which *i == *(i-1) (for the version with no arguments) or pred(*i,*(i - 1)) (for the version with a predicate argument) holds.

Invalidates only the iterators and references to the erased elements.

Returns:The number of elements erased.

Throws:Nothing unless an exception is thrown by the equality comparison or the predicate.

Complexity:If the range [first, last) is not empty, exactly (last - first) - 1 applications of the corresponding predicate, otherwise no applications of the predicate.

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

Preconditions: *this and x are both sorted with respect to the comparator operator< (for the first two overloads) orcomp (for the last two overloads), andget_­allocator() == x.get_­allocator() is true.

Effects:Merges the two sorted ranges [begin(), end()) and[x.begin(), x.end()).

x is empty after the merge.

If an exception is thrown other than by a comparison there are no effects.

Pointers and references to the moved elements of x now refer to those same elements but as members of *this.

Iterators referring to the moved elements will continue to refer to their elements, but they now behave as iterators into *this, not intox.

Complexity:At most distance(begin(), end()) + distance(x.begin(), x.end()) - 1 comparisons.

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

Effects:Sorts the list according to the operator< or the comp function object.

If an exception is thrown, the order of the elements in *this is unspecified.

Does not affect the validity of iterators and references.

Complexity:Approximately comparisons, where N is distance(begin(), end()).

Effects:Reverses the order of the elements in the list.

Does not affect the validity of iterators and references.

22.3.9.7 Erasure [forward.list.erasure]

template<class T, class Allocator, class U> typename forward_list<T, Allocator>::size_type erase(forward_list<T, Allocator>& c, const U& value);

Effects:Equivalent to: return erase_­if(c, [&](auto& elem) { return elem == value; });

template<class T, class Allocator, class Predicate> typename forward_list<T, Allocator>::size_type erase_if(forward_list<T, Allocator>& c, Predicate pred);

Effects:Equivalent to: return c.remove_­if(pred);

22.3.10 Class template list [list]

22.3.10.1 Overview [list.overview]

Alistis a sequence container that supports bidirectional iterators and allows constant time insert and erase operations anywhere within the sequence, with storage management handled automatically.

Unlike vectors and deques, fast random access to list elements is not supported, but many algorithms only need sequential access anyway.

A list meets all of the requirements of a container, of a reversible container (given in two tables in[container.requirements]), of a sequence container, including most of the optional sequence container requirements ([sequence.reqmts]), and of an allocator-aware container (Table 76).

The exceptions are theoperator[]andatmember functions, which are not provided.225

Descriptions are provided here only for operations onlistthat are not described in one of these tables or for operations where there is additional semantic information.

namespace std { template<class T, class Allocator = allocator> class list { public:

using value_type             = T;
using allocator_type         = Allocator;
using pointer                = typename allocator_traits<Allocator>::pointer;
using const_pointer          = typename allocator_traits<Allocator>::const_pointer;
using reference              = value_type&;
using const_reference        = const value_type&;
using size_type              = implementation-defined; 
using difference_type        = implementation-defined; 
using iterator               = implementation-defined; 
using const_iterator         = implementation-defined; 
using reverse_iterator       = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;


list() : list(Allocator()) { }
explicit list(const Allocator&);
explicit list(size_type n, const Allocator& = Allocator());
list(size_type n, const T& value, const Allocator& = Allocator());
template<class InputIterator>
  list(InputIterator first, InputIterator last, const Allocator& = Allocator());
list(const list& x);
list(list&& x);
list(const list&, const Allocator&);
list(list&&, const Allocator&);
list(initializer_list<T>, const Allocator& = Allocator());
~list();
list& operator=(const list& x);
list& operator=(list&& x)
  noexcept(allocator_traits<Allocator>::is_always_equal::value);
list& operator=(initializer_list<T>);
template<class InputIterator>
  void assign(InputIterator first, InputIterator last);
void assign(size_type n, const T& t);
void assign(initializer_list<T>);
allocator_type get_allocator() const noexcept;


iterator               begin() noexcept;
const_iterator         begin() const noexcept;
iterator               end() noexcept;
const_iterator         end() const noexcept;
reverse_iterator       rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
reverse_iterator       rend() noexcept;
const_reverse_iterator rend() const noexcept;

const_iterator         cbegin() const noexcept;
const_iterator         cend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;


[[nodiscard]] bool empty() const noexcept;
size_type size() const noexcept;
size_type max_size() const noexcept;
void      resize(size_type sz);
void      resize(size_type sz, const T& c);


reference       front();
const_reference front() const;
reference       back();
const_reference back() const;


template<class... Args> reference emplace_front(Args&&... args);
template<class... Args> reference emplace_back(Args&&... args);
void push_front(const T& x);
void push_front(T&& x);
void pop_front();
void push_back(const T& x);
void push_back(T&& x);
void pop_back();

template<class... Args> iterator emplace(const_iterator position, Args&&... args);
iterator insert(const_iterator position, const T& x);
iterator insert(const_iterator position, T&& x);
iterator insert(const_iterator position, size_type n, const T& x);
template<class InputIterator>
  iterator insert(const_iterator position, InputIterator first, InputIterator last);
iterator insert(const_iterator position, initializer_list<T> il);

iterator erase(const_iterator position);
iterator erase(const_iterator position, const_iterator last);
void     swap(list&) noexcept(allocator_traits<Allocator>::is_always_equal::value);
void     clear() noexcept;


void splice(const_iterator position, list& x);
void splice(const_iterator position, list&& x);
void splice(const_iterator position, list& x, const_iterator i);
void splice(const_iterator position, list&& x, const_iterator i);
void splice(const_iterator position, list& x, const_iterator first, const_iterator last);
void splice(const_iterator position, list&& x, const_iterator first, const_iterator last);

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

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

void merge(list& x);
void merge(list&& x);
template<class Compare> void merge(list& x, Compare comp);
template<class Compare> void merge(list&& x, Compare comp);

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

void reverse() noexcept;

};

template<class InputIterator, class Allocator = allocator<iter-value-type>> list(InputIterator, InputIterator, Allocator = Allocator()) -> list<iter-value-type, Allocator>;

template<class T, class Allocator> void swap(list<T, Allocator>& x, list<T, Allocator>& y) noexcept(noexcept(x.swap(y))); }

T shall be complete before any member of the resulting specialization of list is referenced.

22.3.10.2 Constructors, copy, and assignment [list.cons]

explicit list(const Allocator&);

Effects:Constructs an empty list, using the specified allocator.

explicit list(size_type n, const Allocator& = Allocator());

Preconditions: T is Cpp17DefaultInsertable into *this.

Effects:Constructs a list withn default-inserted elements using the specified allocator.

list(size_type n, const T& value, const Allocator& = Allocator());

Preconditions: T is Cpp17CopyInsertable into *this.

Effects:Constructs alistwithncopies ofvalue, using the specified allocator.

template<class InputIterator> list(InputIterator first, InputIterator last, const Allocator& = Allocator());

Effects:Constructs alistequal to the range[first, last).

Complexity:Linear indistance(first, last).

22.3.10.3 Capacity [list.capacity]

void resize(size_type sz);

Preconditions: T is Cpp17DefaultInsertable into *this.

Effects:If size() < sz, appends sz - size() default-inserted elements to the sequence.

If sz <= size(), equivalent to:

list::iterator it = begin(); advance(it, sz); erase(it, end());

void resize(size_type sz, const T& c);

Preconditions: T is Cpp17CopyInsertable into *this.

Effects:As if by:

if (sz > size()) insert(end(), sz-size(), c); else if (sz < size()) { iterator i = begin(); advance(i, sz); erase(i, end()); } else ;

22.3.10.4 Modifiers [list.modifiers]

iterator insert(const_iterator position, const T& x); iterator insert(const_iterator position, T&& x); iterator insert(const_iterator position, size_type n, const T& x);template<class InputIterator> iterator insert(const_iterator position, InputIterator first, InputIterator last); iterator insert(const_iterator position, initializer_list<T>);template<class... Args> reference emplace_front(Args&&... args);template<class... Args> reference emplace_back(Args&&... args);template<class... Args> iterator emplace(const_iterator position, Args&&... args);void push_front(const T& x);void push_front(T&& x);void push_back(const T& x);void push_back(T&& x);

Remarks:Does not affect the validity of iterators and references.

If an exception is thrown there are no effects.

Complexity:Insertion of a single element into a list takes constant time and exactly one call to a constructor ofT.

Insertion of multiple elements into a list is linear in the number of elements inserted, and the number of calls to the copy constructor or move constructor of T is exactly equal to the number of elements inserted.

iterator erase(const_iterator position); iterator erase(const_iterator first, const_iterator last);void pop_front();void pop_back();void clear() noexcept;

Effects:Invalidates only the iterators and references to the erased elements.

Complexity:Erasing a single element is a constant time operation with a single call to the destructor ofT.

Erasing a range in a list is linear time in the size of the range and the number of calls to the destructor of typeTis exactly equal to the size of the range.

22.3.10.5 Operations [list.ops]

Since lists allow fast insertion and erasing from the middle of a list, certain operations are provided specifically for them.226

In this subclause, arguments for a template parameter named Predicate or BinaryPredicateshall meet the corresponding requirements in [algorithms.requirements].

For merge and sort, the definitions and requirements in [alg.sorting] apply.

list provides three splice operations that destructively move elements from one list to another.

The behavior of splice operations is undefined if get_­allocator() !=x.get_­allocator().

void splice(const_iterator position, list& x);void splice(const_iterator position, list&& x);

Preconditions: addressof(x) != this is true.

Effects:Inserts the contents ofxbeforepositionandxbecomes empty.

Pointers and references to the moved elements ofxnow refer to those same elements but as members of*this.

Iterators referring to the moved elements will continue to refer to their elements, but they now behave as iterators into*this, not intox.

Complexity:Constant time.

void splice(const_iterator position, list& x, const_iterator i);void splice(const_iterator position, list&& x, const_iterator i);

Preconditions: i is a valid dereferenceable iterator of x.

Effects:Inserts an element pointed to byifrom listxbefore position and removes the element fromx.

The result is unchanged ifposition == iorposition == ++i.

Pointers and references to*icontinue to refer to this same element but as a member of*this.

Iterators to*i(includingiitself) continue to refer to the same element, but now behave as iterators into*this, not intox.

Complexity:Constant time.

void splice(const_iterator position, list& x, const_iterator first, const_iterator last);void splice(const_iterator position, list&& x, const_iterator first, const_iterator last);

Preconditions: [first, last) is a valid range in x.

position is not an iterator in the range [first, last).

Effects:Inserts elements in the range[first, last)beforepositionand removes the elements fromx.

Pointers and references to the moved elements ofxnow refer to those same elements but as members of*this.

Iterators referring to the moved elements will continue to refer to their elements, but they now behave as iterators into*this, not intox.

Complexity:Constant time ifaddressof(x) == this; otherwise, linear time.

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

Effects:Erases all the elements in the list referred to by a list iterator i for which the following conditions hold: *i == value, pred(*i) != false.

Invalidates only the iterators and references to the erased elements.

Returns:The number of elements erased.

Throws:Nothing unless an exception is thrown by*i == valueorpred(*i) != false.

Complexity:Exactlysize()applications of the corresponding predicate.

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

Effects:Erases all but the first element from every consecutive group of equal elements referred to by the iterator i in the range[first + 1, last) for which *i == *(i-1) (for the version ofunique with no arguments) or pred(*i, *(i - 1)) (for the version ofunique with a predicate argument) holds.

Invalidates only the iterators and references to the erased elements.

Returns:The number of elements erased.

Throws:Nothing unless an exception is thrown by*i == *(i-1)orpred(*i, *(i - 1))

Complexity:If the range[first, last)is not empty, exactly(last - first) - 1applications of the corresponding predicate, otherwise no applications of the predicate.

void merge(list& x);void merge(list&& x);template<class Compare> void merge(list& x, Compare comp);template<class Compare> void merge(list&& x, Compare comp);

Preconditions:Both the list and the argument list shall be sorted with respect to the comparator operator< (for the first two overloads) orcomp (for the last two overloads), andget_­allocator() == x.get_­allocator() is true.

Effects:If addressof(x) == this, does nothing; otherwise, merges the two sorted ranges [begin(), end())and [x.​begin(), x.end()).

The result is a range in which the elements will be sorted in non-decreasing order according to the ordering defined by comp; that is, for every iterator i, in the range other than the first, the conditioncomp(*i, *(i - 1)) will be false.

Pointers and references to the moved elements of x now refer to those same elements but as members of *this.

Iterators referring to the moved elements will continue to refer to their elements, but they now behave as iterators into *this, not intox.

If addressof(x) != this, the range [x.begin(), x.end())is empty after the merge.

No elements are copied by this operation.

Complexity:At mostsize() + x.size() - 1applications of comp ifaddressof(x) != this; otherwise, no applications of comp are performed.

If an exception is thrown other than by a comparison there are no effects.

Effects:Reverses the order of the elements in the list.

Does not affect the validity of iterators and references.

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

Effects:Sorts the list according to the operator< or a Compare function object.

If an exception is thrown, the order of the elements in *this is unspecified.

Does not affect the validity of iterators and references.

Complexity:Approximatelycomparisons, whereN == size().

22.3.10.6 Erasure [list.erasure]

template<class T, class Allocator, class U> typename list<T, Allocator>::size_type erase(list<T, Allocator>& c, const U& value);

Effects:Equivalent to: return erase_­if(c, [&](auto& elem) { return elem == value; });

template<class T, class Allocator, class Predicate> typename list<T, Allocator>::size_type erase_if(list<T, Allocator>& c, Predicate pred);

Effects:Equivalent to: return c.remove_­if(pred);

22.3.11 Class template vector [vector]

22.3.11.1 Overview [vector.overview]

Avectoris a sequence container that supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time.

Storage management is handled automatically, though hints can be given to improve efficiency.

A vector meets all of the requirements of a container and of a reversible container (given in two tables in [container.requirements]), of a sequence container, including most of the optional sequence container requirements ([sequence.reqmts]), of an allocator-aware container (Table 76), and, for an element type other than bool, of a contiguous container.

The exceptions are thepush_­front, pop_­front, and emplace_­front member functions, which are not provided.

Descriptions are provided here only for operations on vectorthat are not described in one of these tables or for operations where there is additional semantic information.

namespace std { template<class T, class Allocator = allocator> class vector { public:

using value_type             = T;
using allocator_type         = Allocator;
using pointer                = typename allocator_traits<Allocator>::pointer;
using const_pointer          = typename allocator_traits<Allocator>::const_pointer;
using reference              = value_type&;
using const_reference        = const value_type&;
using size_type              = implementation-defined; 
using difference_type        = implementation-defined; 
using iterator               = implementation-defined; 
using const_iterator         = implementation-defined; 
using reverse_iterator       = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;


constexpr vector() noexcept(noexcept(Allocator())) : vector(Allocator()) { }
constexpr explicit vector(const Allocator&) noexcept;
constexpr explicit vector(size_type n, const Allocator& = Allocator());
constexpr vector(size_type n, const T& value, const Allocator& = Allocator());
template<class InputIterator>
  constexpr vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
constexpr vector(const vector& x);
constexpr vector(vector&&) noexcept;
constexpr vector(const vector&, const Allocator&);
constexpr vector(vector&&, const Allocator&);
constexpr vector(initializer_list<T>, const Allocator& = Allocator());
constexpr ~vector();
constexpr vector& operator=(const vector& x);
constexpr vector& operator=(vector&& x)
  noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
           allocator_traits<Allocator>::is_always_equal::value);
constexpr vector& operator=(initializer_list<T>);
template<class InputIterator>
  constexpr void assign(InputIterator first, InputIterator last);
constexpr void assign(size_type n, const T& u);
constexpr void assign(initializer_list<T>);
constexpr allocator_type get_allocator() const noexcept;


constexpr iterator               begin() noexcept;
constexpr const_iterator         begin() const noexcept;
constexpr iterator               end() noexcept;
constexpr const_iterator         end() const noexcept;
constexpr reverse_iterator       rbegin() noexcept;
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr reverse_iterator       rend() noexcept;
constexpr const_reverse_iterator rend() const noexcept;

constexpr const_iterator         cbegin() const noexcept;
constexpr const_iterator         cend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;


[[nodiscard]] constexpr bool empty() const noexcept;
constexpr size_type size() const noexcept;
constexpr size_type max_size() const noexcept;
constexpr size_type capacity() const noexcept;
constexpr void      resize(size_type sz);
constexpr void      resize(size_type sz, const T& c);
constexpr void      reserve(size_type n);
constexpr void      shrink_to_fit();


constexpr reference       operator[](size_type n);
constexpr const_reference operator[](size_type n) const;
constexpr const_reference at(size_type n) const;
constexpr reference       at(size_type n);
constexpr reference       front();
constexpr const_reference front() const;
constexpr reference       back();
constexpr const_reference back() const;


constexpr T*       data() noexcept;
constexpr const T* data() const noexcept;


template<class... Args> constexpr reference emplace_back(Args&&... args);
constexpr void push_back(const T& x);
constexpr void push_back(T&& x);
constexpr void pop_back();

template<class... Args> constexpr iterator emplace(const_iterator position, Args&&... args);
constexpr iterator insert(const_iterator position, const T& x);
constexpr iterator insert(const_iterator position, T&& x);
constexpr iterator insert(const_iterator position, size_type n, const T& x);
template<class InputIterator>
  constexpr iterator insert(const_iterator position,
                            InputIterator first, InputIterator last);
constexpr iterator insert(const_iterator position, initializer_list<T> il);
constexpr iterator erase(const_iterator position);
constexpr iterator erase(const_iterator first, const_iterator last);
constexpr void     swap(vector&)
  noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value ||
           allocator_traits<Allocator>::is_always_equal::value);
constexpr void     clear() noexcept;

};

template<class InputIterator, class Allocator = allocator<iter-value-type>> vector(InputIterator, InputIterator, Allocator = Allocator()) -> vector<iter-value-type, Allocator>;

template<class T, class Allocator> constexpr void swap(vector<T, Allocator>& x, vector<T, Allocator>& y) noexcept(noexcept(x.swap(y))); }

T shall be complete before any member of the resulting specialization of vector is referenced.

22.3.11.2 Constructors, copy, and assignment [vector.cons]

constexpr explicit vector(const Allocator&) noexcept;

Effects:Constructs an empty vector, using the specified allocator.

constexpr explicit vector(size_type n, const Allocator& = Allocator());

Preconditions: T is Cpp17DefaultInsertable into *this.

Effects:Constructs a vector with ndefault-inserted elements using the specified allocator.

constexpr vector(size_type n, const T& value,const Allocator& = Allocator());

Preconditions: T isCpp17CopyInsertable into *this.

Effects:Constructs a vector with ncopies of value, using the specified allocator.

template<class InputIterator> constexpr vector(InputIterator first, InputIterator last,const Allocator& = Allocator());

Effects:Constructs a vector equal to the range [first, last), using the specified allocator.

Complexity:Makes only Ncalls to the copy constructor ofT(where Nis the distance betweenfirstandlast) and no reallocations if iterators first and last are of forward, bidirectional, or random access categories.

It makes orderNcalls to the copy constructor ofTand orderlogNreallocations if they are just input iterators.

22.3.11.3 Capacity [vector.capacity]

constexpr size_type capacity() const noexcept;

Returns:The total number of elements that the vector can hold without requiring reallocation.

Complexity:Constant time.

constexpr void reserve(size_type n);

Preconditions: T is Cpp17MoveInsertable into *this.

Effects:A directive that informs avectorof a planned change in size, so that it can manage the storage allocation accordingly.

Afterreserve(),capacity()is greater or equal to the argument ofreserveif reallocation happens; and equal to the previous value ofcapacity()otherwise.

Reallocation happens at this point if and only if the current capacity is less than the argument ofreserve().

If an exception is thrown other than by the move constructor of a non-Cpp17CopyInsertable type, there are no effects.

Complexity:It does not change the size of the sequence and takes at most linear time in the size of the sequence.

Throws: length_­error if n >max_­size().227

Remarks:Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence, as well as the past-the-end iterator.

[ Note

:

If no reallocation happens, they remain valid.

end note

]

No reallocation shall take place during insertions that happen after a call to reserve()until an insertion would make the size of the vector greater than the value of capacity().

constexpr void shrink_to_fit();

Preconditions: T is Cpp17MoveInsertable into *this.

Effects: shrink_­to_­fit is a non-binding request to reducecapacity() to size().

[ Note

:

The request is non-binding to allow latitude for implementation-specific optimizations.

end note

]

It does not increase capacity(), but may reduce capacity()by causing reallocation.

If an exception is thrown other than by the move constructor of a non-Cpp17CopyInsertable T there are no effects.

Complexity:If reallocation happens, linear in the size of the sequence.

Remarks:Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence as well as the past-the-end iterator.

[ Note

:

If no reallocation happens, they remain valid.

end note

]

constexpr void swap(vector& x) noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value || allocator_traits<Allocator>::is_always_equal::value);

Effects:Exchanges the contents andcapacity()of*thiswith that of x.

Complexity:Constant time.

constexpr void resize(size_type sz);

Preconditions: T isCpp17MoveInsertable and Cpp17DefaultInsertable into *this.

Effects:If sz < size(), erases the last size() - sz elements from the sequence.

Otherwise, appends sz - size() default-inserted elements to the sequence.

Remarks:If an exception is thrown other than by the move constructor of a non-Cpp17CopyInsertable T there are no effects.

constexpr void resize(size_type sz, const T& c);

Preconditions: T isCpp17CopyInsertable into *this.

Effects:If sz < size(), erases the last size() - sz elements from the sequence.

Otherwise, appends sz - size() copies of c to the sequence.

Remarks:If an exception is thrown there are no effects.

22.3.11.4 Data [vector.data]

constexpr T* data() noexcept;constexpr const T* data() const noexcept;

Returns:A pointer such that [data(), data() + size()) is a valid range.

For a non-empty vector, data() == addressof(front()).

Complexity:Constant time.

22.3.11.5 Modifiers [vector.modifiers]

constexpr iterator insert(const_iterator position, const T& x);constexpr iterator insert(const_iterator position, T&& x);constexpr iterator insert(const_iterator position, size_type n, const T& x);template<class InputIterator> constexpr iterator insert(const_iterator position, InputIterator first, InputIterator last);constexpr iterator insert(const_iterator position, initializer_list<T>);template<class... Args> constexpr reference emplace_back(Args&&... args);template<class... Args> constexpr iterator emplace(const_iterator position, Args&&... args);constexpr void push_back(const T& x);constexpr void push_back(T&& x);

Remarks:Causes reallocation if the new size is greater than the old capacity.

Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence, as well as the past-the-end iterator.

If no reallocation happens, then references, pointers, and iterators before the insertion point remain valid but those at or after the insertion point, including the past-the-end iterator, are invalidated.

If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator ofT or by any InputIterator operation there are no effects.

If an exception is thrown while inserting a single element at the end andT is Cpp17CopyInsertable or is_­nothrow_­move_­constructible_­v<T>is true, there are no effects.

Otherwise, if an exception is thrown by the move constructor of a non-Cpp17CopyInsertable T, the effects are unspecified.

Complexity:If reallocation happens, linear in the number of elements of the resulting vector; otherwise, linear in the number of elements inserted plus the distance to the end of the vector.

constexpr iterator erase(const_iterator position);constexpr iterator erase(const_iterator first, const_iterator last);constexpr void pop_back();

Effects:Invalidates iterators and references at or after the point of the erase.

Complexity:The destructor of T is called the number of times equal to the number of the elements erased, but the assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements.

Throws:Nothing unless an exception is thrown by the assignment operator or move assignment operator ofT.

22.3.11.6 Erasure [vector.erasure]

template<class T, class Allocator, class U> constexpr typename vector<T, Allocator>::size_type erase(vector<T, Allocator>& c, const U& value);

Effects:Equivalent to:

auto it = remove(c.begin(), c.end(), value); auto r = distance(it, c.end()); c.erase(it, c.end()); return r;

template<class T, class Allocator, class Predicate> constexpr typename vector<T, Allocator>::size_type erase_if(vector<T, Allocator>& c, Predicate pred);

Effects:Equivalent to:

auto it = remove_if(c.begin(), c.end(), pred); auto r = distance(it, c.end()); c.erase(it, c.end()); return r;

22.3.12 Class vector [vector.bool]

To optimize space allocation, a specialization of vector forboolelements is provided:

namespace std { template class vector<bool, Allocator> { public:

using value_type             = bool;
using allocator_type         = Allocator;
using pointer                = implementation-defined;
using const_pointer          = implementation-defined;
using const_reference        = bool;
using size_type              = implementation-defined; 
using difference_type        = implementation-defined; 
using iterator               = implementation-defined; 
using const_iterator         = implementation-defined; 
using reverse_iterator       = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;


class reference {
  friend class vector;
  constexpr reference() noexcept;
public:
  constexpr reference(const reference&) = default;
  constexpr ~reference();
  constexpr operator bool() const noexcept;
  constexpr reference& operator=(const bool x) noexcept;
  constexpr reference& operator=(const reference& x) noexcept;
  constexpr void flip() noexcept;   
};


constexpr vector() : vector(Allocator()) { }
constexpr explicit vector(const Allocator&);
constexpr explicit vector(size_type n, const Allocator& = Allocator());
constexpr vector(size_type n, const bool& value, const Allocator& = Allocator());
template<class InputIterator>
  constexpr vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
constexpr vector(const vector& x);
constexpr vector(vector&& x);
constexpr vector(const vector&, const Allocator&);
constexpr vector(vector&&, const Allocator&);
constexpr vector(initializer_list<bool>, const Allocator& = Allocator()));
constexpr ~vector();
constexpr vector& operator=(const vector& x);
constexpr vector& operator=(vector&& x);
constexpr vector& operator=(initializer_list<bool>);
template<class InputIterator>
  constexpr void assign(InputIterator first, InputIterator last);
constexpr void assign(size_type n, const bool& t);
constexpr void assign(initializer_list<bool>);
constexpr allocator_type get_allocator() const noexcept;


constexpr iterator               begin() noexcept;
constexpr const_iterator         begin() const noexcept;
constexpr iterator               end() noexcept;
constexpr const_iterator         end() const noexcept;
constexpr reverse_iterator       rbegin() noexcept;
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr reverse_iterator       rend() noexcept;
constexpr const_reverse_iterator rend() const noexcept;

constexpr const_iterator         cbegin() const noexcept;
constexpr const_iterator         cend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;


[[nodiscard]] constexpr bool empty() const noexcept;
constexpr size_type size() const noexcept;
constexpr size_type max_size() const noexcept;
constexpr size_type capacity() const noexcept;
constexpr void      resize(size_type sz, bool c = false);
constexpr void      reserve(size_type n);
constexpr void      shrink_to_fit();


constexpr reference       operator[](size_type n);
constexpr const_reference operator[](size_type n) const;
constexpr const_reference at(size_type n) const;
constexpr reference       at(size_type n);
constexpr reference       front();
constexpr const_reference front() const;
constexpr reference       back();
constexpr const_reference back() const;


template<class... Args> constexpr reference emplace_back(Args&&... args);
constexpr void push_back(const bool& x);
constexpr void pop_back();
template<class... Args> constexpr iterator emplace(const_iterator position, Args&&... args);
constexpr iterator insert(const_iterator position, const bool& x);
constexpr iterator insert(const_iterator position, size_type n, const bool& x);
template<class InputIterator>
  constexpr iterator insert(const_iterator position,
                            InputIterator first, InputIterator last);
constexpr iterator insert(const_iterator position, initializer_list<bool> il);

constexpr iterator erase(const_iterator position);
constexpr iterator erase(const_iterator first, const_iterator last);
constexpr void swap(vector&);
constexpr static void swap(reference x, reference y) noexcept;
constexpr void flip() noexcept;     
constexpr void clear() noexcept;

}; }

Unless described below, all operations have the same requirements and semantics as the primary vector template, except that operations dealing with the bool value type map to bit values in the container storage andallocator_­traits​::​constructis not used to construct these values.

There is no requirement that the data be stored as a contiguous allocation of bool values.

A space-optimized representation of bits is recommended instead.

referenceis a class that simulates the behavior of references of a single bit invector<bool>.

The conversion function returns truewhen the bit is set, and false otherwise.

The assignment operator sets the bit when the argument is (convertible to) true and clears it otherwise.

flip reverses the state of the bit.

constexpr void flip() noexcept;

Effects:Replaces each element in the container with its complement.

constexpr static void swap(reference x, reference y) noexcept;

Effects:Exchanges the contents of x and y as if by:

bool b = x; x = y; y = b;

template<class Allocator> struct hash<vector<bool, Allocator>>;