The headers <array>, <deque>, <forward_list>, <list>, and <vector> define template classes that meet the requirements for sequence containers.
The headers <queue> and <stack> define container adaptors ([container.adaptors]) that also meet the requirements for sequence containers.
#include <initializer_list> namespace std { template <class T, size_t N > struct array; template <class T, size_t N> bool operator==(const array<T,N>& x, const array<T,N>& y); template <class T, size_t N> bool operator!=(const array<T,N>& x, const array<T,N>& y); template <class T, size_t N> bool operator<(const array<T,N>& x, const array<T,N>& y); template <class T, size_t N> bool operator>(const array<T,N>& x, const array<T,N>& y); template <class T, size_t N> bool operator<=(const array<T,N>& x, const array<T,N>& y); template <class T, size_t N> bool operator>=(const array<T,N>& x, const array<T,N>& y); template <class T, size_t N > void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); template <class T> class tuple_size; template <size_t I, class T> class 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> T& get(array<T, N>&) noexcept; template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; }
#include <initializer_list> namespace std { template <class T, class Allocator = allocator<T> > class deque; template <class T, class Allocator> bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator<(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator>(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool 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); }
Header <forward_list> synopsis
#include <initializer_list> namespace std { template <class T, class Allocator = allocator<T> > 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> bool operator< (const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator!=(const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator> (const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator>=(const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool 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); }
#include <initializer_list> namespace std { template <class T, class Allocator = allocator<T> > class list; template <class T, class Allocator> bool operator==(const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator< (const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator!=(const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator> (const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator>=(const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool 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); }
#include <initializer_list>
namespace std {
template <class T, class Allocator = allocator<T> > class vector;
template <class T, class Allocator>
bool operator==(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
template <class T, class Allocator>
bool operator< (const vector<T,Allocator>& x,const vector<T,Allocator>& y);
template <class T, class Allocator>
bool operator!=(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
template <class T, class Allocator>
bool operator> (const vector<T,Allocator>& x,const vector<T,Allocator>& y);
template <class T, class Allocator>
bool operator>=(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
template <class T, class Allocator>
bool operator<=(const vector<T,Allocator>& x,const vector<T,Allocator>& y);
template <class T, class Allocator>
void swap(vector<T,Allocator>& x, vector<T,Allocator>& y);
template <class Allocator> class vector<bool,Allocator>;
// hash support
template <class T> struct hash;
template <class Allocator> struct hash<vector<bool, Allocator> >;
}
The header <array> defines a class template for storing fixed-size
sequences of objects. An array supports random access iterators. An
instance of array<T, N> stores N elements of type T, so that
size() == N is an invariant. The elements of an array are stored contiguously,
meaning that if a is an array<T, N> then it obeys the identity
&a[n] == &a[0] + n
for all 0 <= n < N.
An array is an aggregate ([dcl.init.aggr]) that can be initialized with the syntax
array<T, N> a = { initializer-list };
where initializer-list is a comma-separated list of up to N elements whose types are convertible to T.
An array satisfies 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. An array satisfies some of the requirements of a sequence container ([sequence.reqmts]). 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.
namespace std { template <class T, size_t N > struct array { // types: typedef T& reference; typedef const T& const_reference; typedef implementation-defined iterator; typedef implementation-defined const_iterator; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T value_type; typedef T* pointer; typedef const T* const_pointer; typedef reverse_iterator<iterator> reverse_iterator; typedef reverse_iterator<const_iterator> const_reverse_iterator; T elems[N]; // exposition only // no explicit construct/copy/destroy for aggregate type void fill(const T& u); void swap(array&) noexcept(noexcept(swap(declval<T&>(), declval<T&>()))); // iterators: 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; // capacity: constexpr size_type size() noexcept; constexpr size_type max_size() noexcept; constexpr bool empty() noexcept; // element access: reference operator[](size_type n); const_reference operator[](size_type n) const; const_reference at(size_type n) const; reference at(size_type n); reference front(); const_reference front() const; reference back(); const_reference back() const; T * data() noexcept; const T * data() const noexcept; }; }
[ Note: The member variable elems is shown for exposition only, to emphasize that array is a class aggregate. The name elems is not part of array's interface. — end note ]
The conditions for an aggregate ([dcl.init.aggr]) shall be met. Class array relies on the implicitly-declared special member functions ([class.ctor], [class.dtor], and [class.copy]) to conform to the container requirements table in [container.requirements]. In addition to the requirements specified in the container requirements table, the implicit move constructor and move assignment operator for array require that T be MoveConstructible or MoveAssignable, respectively.
template <class T, size_t N> void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y)));
Effects:
x.swap(y);
Complexity: linear in N.
template <class T, size_t N> constexpr size_type array<T,N>::size() noexcept;
Returns: N
T *data() noexcept;
const T *data() const noexcept;
Returns: elems.
void swap(array& y) noexcept(noexcept(swap(declval<T&>(), declval<T&>())));
Effects: swap_ranges(begin(), end(), y.begin())
Throws: Nothing unless one of the element-wise swap calls throws an exception.
Note: Unlike the swap function for other containers, array::swap takes linear time, may exit via an exception, and does not cause iterators to become associated with the other container.
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 noexcept-specification which is equivalent to noexcept(true).
tuple_size<array<T, N> >::value
Return type: integral constant expression.
Value: N
tuple_element<I, array<T, N> >::type
Requires: I < N. The program is ill-formed if I is out of bounds.
Value: The type T.
template <size_t I, class T, size_t N> T& get(array<T, N>& a) noexcept;
Requires: I < N. The program is ill-formed if I is out of bounds.
Returns: A reference to the Ith element of a, where indexing is zero-based.
template <size_t I, class T, size_t N> T&& get(array<T, N>&& a) noexcept;
Effects: Equivalent to return std::move(get<I>(a));
template <size_t I, class T, size_t N> const T& get(const array<T, N>& a) noexcept;
Requires: I < N. The program is ill-formed if I is out of bounds.
Returns: A const reference to the Ith element of a, where indexing is zero-based.
A deque is a sequence container that, like a vector ([vector]), supports random access iterators. 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. As with vectors, storage management is handled automatically.
A deque satisfies 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 [tab:containers.allocatoraware]). Descriptions are provided here only for operations on deque that 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<T> > class deque { public: // types: typedef value_type& reference; typedef const value_type& const_reference; typedef implementation-defined iterator; // See [container.requirements] typedef implementation-defined const_iterator; // See [container.requirements] typedef implementation-defined size_type; // See [container.requirements] typedef implementation-defined difference_type;// See [container.requirements] typedef T value_type; typedef Allocator allocator_type; typedef typename allocator_traits<Allocator>::pointer pointer; typedef typename allocator_traits<Allocator>::const_pointer const_pointer; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; // [deque.cons], construct/copy/destroy: explicit deque(const Allocator& = Allocator()); explicit deque(size_type n); 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); 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; // iterators: 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; // [deque.capacity], capacity: 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(); bool empty() const noexcept; // element access: 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; // [deque.modifiers], modifiers: template <class... Args> void emplace_front(Args&&... args); template <class... Args> void 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&); void clear() noexcept; }; template <class T, class Allocator> bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); template <class T, class Allocator> bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y); // specialized algorithms: template <class T, class Allocator> void swap(deque<T,Allocator>& x, deque<T,Allocator>& y); }
explicit deque(const Allocator& = Allocator());
Effects: Constructs an empty deque, using the specified allocator.
Complexity: Constant.
Effects: Constructs a deque with n value-initialized elements.
Requires: T shall be DefaultConstructible.
Complexity: Linear in n.
deque(size_type n, const T& value,
const Allocator& = Allocator());
Effects: Constructs a deque with n copies of value, using the specified allocator.
Requires: T shall be CopyInsertable into *this.
Complexity: Linear in n.
template <class InputIterator>
deque(InputIterator first, InputIterator last,
const Allocator& = Allocator());
Effects: Constructs a deque equal to the range [first,last), using the specified allocator.
Complexity: distance(first, last).
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
Effects:
erase(begin(), end()); insert(begin(), first, last);
void assign(size_type n, const T& t);
Effects:
erase(begin(), end()); insert(begin(), n, t);
Effects: If sz <= size(), equivalent to erase(begin() + sz, end());. If size() < sz, appends sz - size() value-initialized elements to the sequence.
Requires: T shall be DefaultConstructible.
void resize(size_type sz, const T& c);
Effects:
if (sz > size())
insert(end(), sz-size(), c);
else if (sz < size())
erase(begin()+sz, end());
else
; // do nothing
Requires: T shall be CopyInsertable into *this.
Remarks: shrink_to_fit is a non-binding request to reduce memory use. [ Note: The request is non-binding to allow latitude for implementation-specific optimizations. — end note ]
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> void emplace_front(Args&&... args);
template <class... Args> void 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 of T there are no effects. If an exception is thrown by the move constructor of a non-CopyInsertable 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 either at the beginning or end of a deque always takes constant time and causes a single call to a constructor of T.
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
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 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.
Complexity: The number of calls to the destructor is the same as the number of elements erased, but the number of calls to the assignment operator 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 copy constructor, move constructor, assignment operator, or move assignment operator of T.
template <class T, class Allocator>
void swap(deque<T,Allocator>& x, deque<T,Allocator>& y);
Effects:
x.swap(y);
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 satisfies all of the requirements of a container (Table [tab:containers.container.requirements]), except that the size() member function is not provided. A forward_list also satisfies all of the requirements for an allocator-aware container (Table [tab:containers.allocatoraware]). In addition, a forward_list provides the assign member functions (Table [tab:containers.sequence.requirements]) and several of the optional container requirements (Table [tab:containers.sequence.optional]). Descriptions are provided here only for operations on forward_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 acess a preceding element. For this reason, ranges that are modified, such as those supplied to erase and splice, must be open at the beginning. — end note ]
namespace std { template <class T, class Allocator = allocator<T> > class forward_list { public: // types: typedef value_type& reference; typedef const value_type& const_reference; typedef implementation-defined iterator; // See [container.requirements] typedef implementation-defined const_iterator; // See [container.requirements] typedef implementation-defined size_type; // See [container.requirements] typedef implementation-defined difference_type;// See [container.requirements] typedef T value_type; typedef Allocator allocator_type; typedef typename allocator_traits<Allocator>::pointer pointer; typedef typename allocator_traits<Allocator>::const_pointer const_pointer; // [forwardlist.cons], construct/copy/destroy: explicit forward_list(const Allocator& = Allocator()); explicit forward_list(size_type n); 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); 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; // [forwardlist.iter], iterators: 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; // capacity: bool empty() const noexcept; size_type max_size() const noexcept; // [forwardlist.access], element access: reference front(); const_reference front() const; // [forwardlist.modifiers], modifiers: template <class... Args> void 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&); void resize(size_type sz); void resize(size_type sz, const value_type& c); void clear() noexcept; // [forwardlist.ops], forward_list operations: 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); void remove(const T& value); template <class Predicate> void remove_if(Predicate pred); void unique(); template <class BinaryPredicate> void 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; }; // Comparison operators template <class T, class Allocator> bool operator==(const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator< (const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator!=(const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator> (const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator>=(const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); template <class T, class Allocator> bool operator<=(const forward_list<T,Allocator>& x, const forward_list<T,Allocator>& y); // [forwardlist.spec], specialized algorithms: template <class T, class Allocator> void swap(forward_list<T,Allocator>& x, forward_list<T,Allocator>& y); }
explicit forward_list(const Allocator& = Allocator());
Effects: Constructs an empty forward_list object using the specified allocator.
Complexity: Constant.
explicit forward_list(size_type n);
Effects: Constructs a forward_list object with n value-initialized elements.
Requires: T shall be DefaultConstructible.
Complexity: Linear in n.
forward_list(size_type n, const T& value, const Allocator& = Allocator());
Effects: Constructs a forward_list object with n copies of value using the specified allocator.
Requires: T shall be CopyInsertable into *this.
Complexity: Linear in n.
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).
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
Effects: clear(); insert_after(before_begin(), first, last);
void assign(size_type n, const T& t);
Effects: clear(); insert_after(before_begin(), n, t);
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 to const_cast<forward_list const&>(*this).before_begin().
Remarks: before_begin() == end() shall equal false.
reference front();
const_reference front() const;
Returns: *begin()
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 in n, 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> void emplace_front(Args&&... args);
Effects: Inserts an object of type value_type constructed with value_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: erase_after(before_begin())
iterator insert_after(const_iterator position, const T& x);
iterator insert_after(const_iterator position, T&& x);
Requires: 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);
Requires: 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);
Requires: position is before_begin() or is a dereferenceable iterator in the range [begin(),end()). first and last are not 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 i1 is empty.
template <class... Args>
iterator emplace_after(const_iterator position, Args&&... args);
Requires: position is before_begin() or is a dereferenceable iterator in the range [begin(),end()).
Effects: Inserts an object of type value_type constructed with value_type(std::forward<Args>(args)...) after position.
Requires: 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.
Throws: Nothing.
iterator erase_after(const_iterator position, const_iterator last);
Requires: All iterators in the range (position,last) are dereferenceable.
Effects: Erases the elements in the range (position,last).
Returns: last.
Throws: Nothing.
void resize(size_type sz);
void resize(size_type sz, const value_type& c);
Effects: If sz < distance(begin(), end()), erases the last distance(begin(), end()) - sz elements from the list. Otherwise, inserts sz - distance(begin(), end()) elements at the end of the list. For the first signature the inserted elements are value-initialized, and for the second signature they are copies of c.
Requires: T shall be DefaultConstructible for the first form and it shall be CopyInsertable into *this for the second form.
Effects: Erases all elements in the range [begin(),end()).
Remarks: Does not invalidate past-the-end iterators.
void splice_after(const_iterator position, forward_list& x);
void splice_after(const_iterator position, forward_list&& x);
Requires: position is before_begin() or is a dereferenceable iterator in the range [begin(),end()). &x != this.
Effects: Inserts the contents of x after position, 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.
Throws: Nothing.
Complexity: Ο(distance(x.begin(), x.end()))
void splice_after(const_iterator position, forward_list& x, const_iterator i);
void splice_after(const_iterator position, forward_list&& x, const_iterator i);
Requires: position is before_begin() or is a dereferenceable iterator in the range [begin(),end()). The iterator following i is a dereferenceable iterator in x.
Effects: Inserts the element following i into *this, following position, 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 (including i itself) continue to refer to the same element, but now behave as iterators into *this, not into x.
Throws: Nothing.
Complexity: Ο(1)
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);
Requires: 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).
Effects: Inserts elements in the range (first,last) after position and removes the elements from x. 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.
Complexity: Ο(distance(first, last))
void remove(const T& value);
template <class Predicate> void remove_if(Predicate pred);
Effects: Erases all the elements in the list referred by a list iterator i for which the following conditions hold: *i == value (for remove()), pred(*i) is true (for remove_if()). This operation shall be stable: the relative order of the elements that are not removed is the same as their relative order in the original list. Invalidates only the iterators and references to the erased elements.
Throws: Nothing unless an exception is thrown by the equality comparison or the predicate.
Complexity: Exactly distance(begin(), end()) applications of the corresponding predicate.
void unique();
template <class BinaryPredicate> void 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.
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)
Requires: comp defines a strict weak ordering ([alg.sorting]), and *this and x are both sorted according to this ordering.
Effects: Merges x into *this. This operation shall be stable: for equivalent elements in the two lists, the elements from *this shall always precede the elements from x. 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 into x.
Complexity: At most distance(begin(), end()) + distance(x.begin(), x.end()) - 1 comparisons.
void sort();
template <class Compare> void sort(Compare comp);
Requires: operator< (for the version with no arguments) or comp (for the version with a comparison argument) defines a strict weak ordering ([alg.sorting]).
Effects: Sorts the list according to the operator< or the comp function object. This operation shall be stable: the relative order of the equivalent elements is preserved. 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 N log N 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.
Complexity: Linear time.
template <class T, class Allocator>
void swap(forward_list<T,Allocator>& x, forward_list<T,Allocator>& y);
Effects: x.swap(y)
A list is 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 ([vector]) and deques ([deque]), fast random access to list elements is not supported, but many algorithms only need sequential access anyway.
A list satisfies 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 [tab:containers.allocatoraware]). The exceptions are the operator[] and at member functions, which are not provided.265 Descriptions are provided here only for operations on list that 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<T> > class list { public: // types: typedef value_type& reference; typedef const value_type& const_reference; typedef implementation-defined iterator; // see [container.requirements] typedef implementation-defined const_iterator; // see [container.requirements] typedef implementation-defined size_type; // see [container.requirements] typedef implementation-defined difference_type;// see [container.requirements] typedef T value_type; typedef Allocator allocator_type; typedef typename allocator_traits<Allocator>::pointer pointer; typedef typename allocator_traits<Allocator>::const_pointer const_pointer; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; // [list.cons], construct/copy/destroy: explicit list(const Allocator& = Allocator()); explicit list(size_type n); 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); 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; // iterators: 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; // [list.capacity], capacity: 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); // element access: reference front(); const_reference front() const; reference back(); const_reference back() const; // [list.modifiers], modifiers: template <class... Args> void emplace_front(Args&&... args); void pop_front(); template <class... Args> void emplace_back(Args&&... args); void push_front(const T& x); void push_front(T&& x); 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&); void clear() noexcept; // [list.ops], list operations: 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); void remove(const T& value); template <class Predicate> void remove_if(Predicate pred); void unique(); template <class BinaryPredicate> void 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 T, class Allocator> bool operator==(const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator< (const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator!=(const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator> (const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator>=(const list<T,Allocator>& x, const list<T,Allocator>& y); template <class T, class Allocator> bool operator<=(const list<T,Allocator>& x, const list<T,Allocator>& y); // specialized algorithms: template <class T, class Allocator> void swap(list<T,Allocator>& x, list<T,Allocator>& y); }
These member functions are only provided by containers whose iterators are random access iterators.
explicit list(const Allocator& = Allocator());
Effects: Constructs an empty list, using the specified allocator.
Complexity: Constant.
Effects: Constructs a list with n value-initialized elements.
Requires: T shall be DefaultConstructible.
Complexity: Linear in n.
list(size_type n, const T& value,
const Allocator& = Allocator());
Effects: Constructs a list with n copies of value, using the specified allocator.
Requires: T shall be CopyInsertable into *this.
Complexity: Linear in n.
template <class InputIterator>
list(InputIterator first, InputIterator last,
const Allocator& = Allocator());
Effects: Constructs a list equal to the range [first,last).
Complexity: Linear in distance(first, last).
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
Effects: Replaces the contents of the list with the range [first, last).
void assign(size_type n, const T& t);
Effects: Replaces the contents of the list with n copies of t.
Effects: If size() < sz, appends sz - size() value-initialized elements to the sequence. If sz <= size(), equivalent to
list<T>::iterator it = begin(); advance(it, sz); erase(it, end());
Requires: T shall be DefaultConstructible.
void resize(size_type sz, const T& c);
Effects:
if (sz > size())
insert(end(), sz-size(), c);
else if (sz < size()) {
iterator i = begin();
advance(i, sz);
erase(i, end());
}
else
; // do nothing
Requires: T shall be CopyInsertable into *this.
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> void emplace_front(Args&&... args);
template <class... Args> void 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 of T. 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.
Throws: Nothing.
Complexity: Erasing a single element is a constant time operation with a single call to the destructor of T. Erasing a range in a list is linear time in the size of the range and the number of calls to the destructor of type T is exactly equal to the size of the range.
Since lists allow fast insertion and erasing from the middle of a list, certain operations are provided specifically for them.266
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);
Requires: &x != this.
Effects: Inserts the contents of x before position 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.
Throws: Nothing.
Complexity: Constant time.
void splice(const_iterator position, list& x, const_iterator i);
void splice(const_iterator position, list&& x, const_iterator i);
Effects: Inserts an element pointed to by i from list x before position and removes the element from x. The result is unchanged if position == i or position == ++i. Pointers and references to *i continue to refer to this same element but as a member of *this. Iterators to *i (including i itself) continue to refer to the same element, but now behave as iterators into *this, not into x.
Requires: i is a valid dereferenceable iterator of x.
Throws: Nothing.
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);
Effects: Inserts elements in the range [first,last) before position and removes the elements from x.
Requires: [first, last) is a valid range in x. The result is undefined if position is an iterator in the range [first,last). 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.
Throws: Nothing.
Complexity: Constant time if &x == this; otherwise, linear time.
void remove(const T& value);
template <class Predicate> void remove_if(Predicate pred);
Effects: Erases all the elements in the list referred 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.
Throws: Nothing unless an exception is thrown by *i == value or pred(*i) != false.
Remarks: Stable.
Complexity: Exactly size() applications of the corresponding predicate.
void unique();
template <class BinaryPredicate> void 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 of unique with no arguments) or pred(*i, *(i - 1)) (for the version of unique with a predicate argument) holds. Invalidates only the iterators and references to the erased elements.
Throws: Nothing unless an exception in thrown by *i == *(i-1) or pred(*i, *(i - 1))
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(list& x);
void merge(list&& x);
template <class Compare> void merge(list& x, Compare comp);
template <class Compare> void merge(list&& x, Compare comp);
Requires: comp shall define a strict weak ordering ([alg.sorting]), and both the list and the argument list shall be sorted according to this ordering.
Effects: If (&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 condition comp(*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 into x.
Remarks: Stable. If (&x != this) the range [x.begin(), x.end()) is empty after the merge. No elements are copied by this operation. The behavior is undefined if this->get_allocator() != x.get_allocator().
Complexity: At most size() + x.size() - 1 applications of comp if (&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.
Complexity: Linear time.
void sort();
template <class Compare> void sort(Compare comp);
Requires: operator< (for the first version) or comp (for the second version) shall define a strict weak ordering ([alg.sorting]).
Effects: Sorts the list according to the operator< or a Compare function object. Does not affect the validity of iterators and references.
Remarks: Stable.
Complexity: Approximately N log(N) comparisons, where N == size().
As specified in [allocator.requirements], the requirements in this Clause apply only to lists whose allocators compare equal.
template <class T, class Allocator>
void swap(list<T,Allocator>& x, list<T,Allocator>& y);
Effects:
x.swap(y);
A vector is a sequence container that supports random access iterators. In addition, it 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. The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().
A vector satisfies 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]), and of an allocator-aware container (Table [tab:containers.allocatoraware]). The exceptions are the push_front, pop_front, and emplace_front member functions, which are not provided. Descriptions are provided here only for operations on vector that 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<T> > class vector { public: // types: typedef value_type& reference; typedef const value_type& const_reference; typedef implementation-defined iterator; // see [container.requirements] typedef implementation-defined const_iterator; // see [container.requirements] typedef implementation-defined size_type; // see [container.requirements] typedef implementation-defined difference_type;// see [container.requirements] typedef T value_type; typedef Allocator allocator_type; typedef typename allocator_traits<Allocator>::pointer pointer; typedef typename allocator_traits<Allocator>::const_pointer const_pointer; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; // [vector.cons], construct/copy/destroy: explicit vector(const Allocator& = Allocator()); explicit vector(size_type n); vector(size_type n, const T& value, const Allocator& = Allocator()); template <class InputIterator> vector(InputIterator first, InputIterator last, const Allocator& = Allocator()); vector(const vector& x); vector(vector&&); vector(const vector&, const Allocator&); vector(vector&&, const Allocator&); vector(initializer_list<T>, const Allocator& = Allocator()); ~vector(); vector& operator=(const vector& x); vector& operator=(vector&& x); vector& operator=(initializer_list<T>); template <class InputIterator> void assign(InputIterator first, InputIterator last); void assign(size_type n, const T& u); void assign(initializer_list<T>); allocator_type get_allocator() const noexcept; // iterators: 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; // [vector.capacity], capacity: size_type size() const noexcept; size_type max_size() const noexcept; void resize(size_type sz); void resize(size_type sz, const T& c); size_type capacity() const noexcept; bool empty() const noexcept; void reserve(size_type n); void shrink_to_fit(); // element access: reference operator[](size_type n); const_reference operator[](size_type n) const; const_reference at(size_type n) const; reference at(size_type n); reference front(); const_reference front() const; reference back(); const_reference back() const; // [vector.data], data access T* data() noexcept; const T* data() const noexcept; // [vector.modifiers], modifiers: template <class... Args> void emplace_back(Args&&... args); 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 first, const_iterator last); void swap(vector&); void clear() noexcept; }; template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y); template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y); template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y); template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); // [vector.special], specialized algorithms: template <class T, class Allocator> void swap(vector<T,Allocator>& x, vector<T,Allocator>& y); }
explicit vector(const Allocator& = Allocator());
Effects: Constructs an empty vector, using the specified allocator.
Complexity: Constant.
Effects: Constructs a vector with n value-initialized elements.
Requires: T shall be DefaultConstructible.
Complexity: Linear in n.
vector(size_type n, const T& value,
const Allocator& = Allocator());
Effects: Constructs a vector with n copies of value, using the specified allocator.
Requires: T shall be CopyInsertable into *this.
Complexity: Linear in n.
template <class InputIterator>
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 N calls to the copy constructor of T (where N is the distance between first and last) and no reallocations if iterators first and last are of forward, bidirectional, or random access categories. It makes order N calls to the copy constructor of T and order log(N) reallocations if they are just input iterators.
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
Effects:
erase(begin(), end()); insert(begin(), first, last);
void assign(size_type n, const T& t);
Effects:
erase(begin(), end()); insert(begin(), n, t);
size_type capacity() const noexcept;
Returns: The total number of elements that the vector can hold without requiring reallocation.
Effects: A directive that informs a vector of a planned change in size, so that it can manage the storage allocation accordingly. After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise. Reallocation happens at this point if and only if the current capacity is less than the argument of reserve(). If an exception is thrown other than by the move constructor of a non-CopyInsertable 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.
Remarks: Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence. It is guaranteed that no reallocation takes place during insertions that happen after a call to reserve() until the time when an insertion would make the size of the vector greater than the value of capacity().
Remarks: shrink_to_fit is a non-binding request to reduce capacity() to size(). [ Note: The request is non-binding to allow latitude for implementation-specific optimizations. — end note ]
Effects: Exchanges the contents and capacity() of *this with that of x.
Complexity: Constant time.
Effects: If sz <= size(), equivalent to erase(begin() + sz, end());. If size() < sz, appends sz - size() value-initialized elements to the sequence.
Requires: T shall be CopyInsertable into *this.
void resize(size_type sz, const T& c);
Effects:
if (sz > size())
insert(end(), sz-size(), c);
else if (sz < size())
erase(begin()+sz, end());
else
; // do nothing
Requires: If an exception is thrown other than by the move constructor of a non-CopyInsertable T there are no effects.
reserve() uses Allocator::allocate() which may throw an appropriate exception.
T* data() noexcept;
const T* data() const noexcept;
Returns: A pointer such that [data(),data() + size()) is a valid range. For a non-empty vector, data() == &front().
Complexity: Constant time.
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> void emplace_back(Args&&... args);
template <class... Args> iterator emplace(const_iterator position, Args&&... args);
void push_back(const T& x);
void push_back(T&& x);
Remarks: Causes reallocation if the new size is greater than the old capacity. If no reallocation happens, all the iterators and references before the insertion point remain valid. If an exception is thrown other than by the copy constructor, move constructor, assignment operator, or move assignment operator of T or by any InputIterator operation there are no effects. If an exception is thrown by the move constructor of a non-CopyInsertable T, the effects are unspecified.
Complexity: The complexity is linear in the number of elements inserted plus the distance to the end of the vector.
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
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 move 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 copy constructor, move constructor, assignment operator, or move assignment operator of T.
template <class T, class Allocator>
void swap(vector<T,Allocator>& x, vector<T,Allocator>& y);
Effects:
x.swap(y);
To optimize space allocation, a specialization of vector for bool elements is provided:
namespace std { template <class Allocator> class vector<bool, Allocator> { public: // types: typedef bool const_reference; typedef implementation-defined iterator; // see [container.requirements] typedef implementation-defined const_iterator; // see [container.requirements] typedef implementation-defined size_type; // see [container.requirements] typedef implementation-defined difference_type;// see [container.requirements] typedef bool value_type; typedef Allocator allocator_type; typedef implementation-defined pointer; typedef implementation-defined const_pointer; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; // bit reference: class reference { friend class vector; reference() noexcept; public: ~reference(); operator bool() const noexcept; reference& operator=(const bool x) noexcept; reference& operator=(const reference& x) noexcept; void flip() noexcept; // flips the bit }; // construct/copy/destroy: explicit vector(const Allocator& = Allocator()); explicit vector(size_type n, const bool& value = bool(), const Allocator& = Allocator()); template <class InputIterator> vector(InputIterator first, InputIterator last, const Allocator& = Allocator()); vector(const vector<bool,Allocator>& x); vector(vector<bool,Allocator>&& x); vector(const vector&, const Allocator&); vector(vector&&, const Allocator&); vector(initializer_list<bool>, const Allocator& = Allocator())); ~vector(); vector<bool,Allocator>& operator=(const vector<bool,Allocator>& x); vector<bool,Allocator>& operator=(vector<bool,Allocator>&& x); vector operator=(initializer_list<bool>); template <class InputIterator> void assign(InputIterator first, InputIterator last); void assign(size_type n, const bool& t); void assign(initializer_list<bool>; allocator_type get_allocator() const noexcept; // iterators: 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; // capacity: size_type size() const noexcept; size_type max_size() const noexcept; void resize(size_type sz, bool c = false); size_type capacity() const noexcept; bool empty() const noexcept; void reserve(size_type n); void shrink_to_fit(); // element access: reference operator[](size_type n); const_reference operator[](size_type n) const; const_reference at(size_type n) const; reference at(size_type n); reference front(); const_reference front() const; reference back(); const_reference back() const; // modifiers: void push_back(const bool& x); void pop_back(); iterator insert(const_iterator position, const bool& x); iterator insert (const_iterator position, size_type n, const bool& x); template <class InputIterator> iterator insert(const_iterator position, InputIterator first, InputIterator last); iterator insert(const_iterator position, initializer_list<bool> il); iterator erase(const_iterator position); iterator erase(const_iterator first, const_iterator last); void swap(vector<bool,Allocator>&); static void swap(reference x, reference y) noexcept; void flip() noexcept; // flips all bits 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 and allocator_traits::construct ([allocator.traits.members]) is 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.
reference is a class that simulates the behavior of references of a single bit in vector<bool>. The conversion operator returns true when 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.
Effects: Replaces each element in the container with its complement.
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> >;
Requires: the template specialization shall meet the requirements of class template hash ([unord.hash]).