24 Containers library [containers]

24.2 Requirements [container.requirements]

24.2.2 General containers [container.requirements.general]

24.2.2.2 Container requirements [container.reqmts]

A type X meets the container requirements if the following types, statements, and expressions are well-formed and have the specified semantics.
typename X::value_type
Result: T
Preconditions: T is Cpp17Erasable from X (see [container.alloc.reqmts], below).
typename X::reference
Result: T&
typename X::const_reference
Result: const T&
typename X::iterator
Result: A type that meets the forward iterator requirements ([forward.iterators]) with value type T.
The type X​::​iterator is convertible to X​::​const_iterator.
typename X::const_iterator
Result: A type that meets the requirements of a constant iterator and those of a forward iterator with value type T.
typename X::difference_type
Result: A signed integer type, identical to the difference type of X​::​iterator and X​::​const_iterator.
typename X::size_type
Result: An unsigned integer type that can represent any non-negative value of X​::​difference_type.
X u; X u = X();
Postconditions: u.empty()
Complexity: Constant.
X u(v); X u = v;
Preconditions: T is Cpp17CopyInsertable into X (see below).
Postconditions: u == v.
Complexity: Linear.
X u(rv); X u = rv;
Postconditions: u is equal to the value that rv had before this construction.
Complexity: Linear for array and inplace_vector and constant for all other standard containers.
t = v;
Result: X&.
Postconditions: t == v.
Complexity: Linear.
t = rv
Result: X&.
Effects: All existing elements of t are either move assigned to or destroyed.
Postconditions: If t and rv do not refer to the same object, t is equal to the value that rv had before this assignment.
Complexity: Linear.
a.~X()
Result: void.
Effects: Destroys every element of a; any memory obtained is deallocated.
Complexity: Linear.
b.begin()
Result: iterator; const_iterator for constant b.
Returns: An iterator referring to the first element in the container.
Complexity: Constant.
b.end()
Result: iterator; const_iterator for constant b.
Returns: An iterator which is the past-the-end value for the container.
Complexity: Constant.
b.cbegin()
Result: const_iterator.
Returns: const_cast<X const&>(b).begin()
Complexity: Constant.
b.cend()
Result: const_iterator.
Returns: const_cast<X const&>(b).end()
Complexity: Constant.
i <=> j
Result: strong_ordering.
Constraints: X​::​iterator meets the random access iterator requirements.
Complexity: Constant.
c == b
Preconditions: T meets the Cpp17EqualityComparable requirements.
Result: bool.
Returns: equal(c.begin(), c.end(), b.begin(), b.end())
[Note 1: 
The algorithm equal is defined in [alg.equal].
— end note]
Complexity: Constant if c.size() != b.size(), linear otherwise.
Remarks: == is an equivalence relation.
c != b
Effects: Equivalent to !(c == b).
t.swap(s)
Result: void.
Effects: Exchanges the contents of t and s.
Complexity: Linear for array and inplace_vector, and constant for all other standard containers.
swap(t, s)
Effects: Equivalent to t.swap(s).
c.size()
Result: size_type.
Returns: distance(c.begin(), c.end()), i.e., the number of elements in the container.
Complexity: Constant.
Remarks: The number of elements is defined by the rules of constructors, inserts, and erases.
c.max_size()
Result: size_type.
Returns: distance(begin(), end()) for the largest possible container.
Complexity: Constant.
c.empty()
Result: bool.
Returns: c.begin() == c.end()
Complexity: Constant.
Remarks: If the container is empty, then c.empty() is true.
In the expressions i == j i != j i < j i <= j i >= j i > j i <=> j i - j where i and j denote objects of a container's iterator type, either or both may be replaced by an object of the container's const_iterator type referring to the same element with no change in semantics.
Unless otherwise specified, all containers defined in this Clause obtain memory using an allocator (see [allocator.requirements]).
[Note 2: 
In particular, containers and iterators do not store references to allocated elements other than through the allocator's pointer type, i.e., as objects of type P or pointer_traits<P>​::​template rebind<unspecified>, where P is allocator_traits<allocator_type>​::​pointer.
— end note]
Copy constructors for these container types obtain an allocator by calling allocator_traits<allocator_type>​::​select_on_container_copy_construction on the allocator belonging to the container being copied.
Move constructors obtain an allocator by move construction from the allocator belonging to the container being moved.
Such move construction of the allocator shall not exit via an exception.
All other constructors for these container types take a const allocator_type& argument.
[Note 3: 
If an invocation of a constructor uses the default value of an optional allocator argument, then the allocator type must support value-initialization.
— end note]
A copy of this allocator is used for any memory allocation and element construction performed, by these constructors and by all member functions, during the lifetime of each container object or until the allocator is replaced.
The allocator may be replaced only via assignment or swap().
Allocator replacement is performed by copy assignment, move assignment, or swapping of the allocator only if
  • allocator_traits<allocator_type>​::​propagate_on_container_copy_assignment​::​value,
  • allocator_traits<allocator_type>​::​propagate_on_container_move_assignment​::​value, or
  • allocator_traits<allocator_type>​::​propagate_on_container_swap​::​value
is true within the implementation of the corresponding container operation.
In all container types defined in this Clause, the member get_allocator() returns a copy of the allocator used to construct the container or, if that allocator has been replaced, a copy of the most recent replacement.
The expression a.swap(b), for containers a and b of a standard container type other than array and inplace_vector, shall exchange the values of a and b without invoking any move, copy, or swap operations on the individual container elements.
Any Compare, Pred, or Hash types belonging to a and b shall meet the Cpp17Swappable requirements and shall be exchanged by calling swap as described in [swappable.requirements].
If allocator_traits<allocator_type>​::​propagate_on_container_swap​::​value is true, then allocator_type shall meet the Cpp17Swappable requirements and the allocators of a and b shall also be exchanged by calling swap as described in [swappable.requirements].
Otherwise, the allocators shall not be swapped, and the behavior is undefined unless a.get_allocator() == b.get_allocator().
Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap.
It is unspecified whether an iterator with value a.end() before the swap will have value b.end() after the swap.
Unless otherwise specified (see [associative.reqmts.except], [unord.req.except], [deque.modifiers], [inplace.vector.modifiers], and [vector.modifiers]) all container types defined in this Clause meet the following additional requirements:
  • If an exception is thrown by an insert() or emplace() function while inserting a single element, that function has no effects.
  • If an exception is thrown by a push_back(), push_front(), emplace_back(), or emplace_front() function, that function has no effects.
  • No erase(), clear(), pop_back() or pop_front() function throws an exception.
  • No copy constructor or assignment operator of a returned iterator throws an exception.
  • No swap() function throws an exception.
  • No swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped.
    [Note 4: 
    The end() iterator does not refer to any element, so it can be invalidated.
    — end note]
Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container.
A contiguous container is a container whose member types iterator and const_iterator meet the Cpp17RandomAccessIterator requirements ([random.access.iterators]) and model contiguous_iterator ([iterator.concept.contiguous]).
The behavior of certain container member functions and deduction guides depends on whether types qualify as input iterators or allocators.
The extent to which an implementation determines that a type cannot be an input iterator is unspecified, except that as a minimum integral types shall not qualify as input iterators.
Likewise, the extent to which an implementation determines that a type cannot be an allocator is unspecified, except that as a minimum a type A shall not qualify as an allocator unless it meets both of the following conditions:
  • The qualified-id A​::​value_type is valid and denotes a type ([temp.deduct]).
  • The expression declval<A&>().allocate(size_t{}) is well-formed when treated as an unevaluated operand.