Files
2025-10-25 03:02:53 +03:00

111 lines
4.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[vector.modifiers]
# 23 Containers library [[containers]](./#containers)
## 23.3 Sequence containers [[sequences]](sequences#vector.modifiers)
### 23.3.13 Class template vector [[vector]](vector#modifiers)
#### 23.3.13.5 Modifiers [vector.modifiers]
[🔗](#lib:insert,vector)
`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);
template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<T> R>
constexpr iterator insert_range(const_iterator position, R&& rg);
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);
template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1Introduction[container.intro.reqmts]")<T> R>
constexpr void append_range(R&& rg);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10328)
*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[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10336)
*Remarks*: Causes reallocation if the new size is greater than the old capacity[.](#2.sentence-1)
Reallocation invalidates all the references, pointers, and iterators
referring to the elements in the sequence, as well as the past-the-end iterator[.](#2.sentence-2)
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[.](#2.sentence-3)
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[.](#2.sentence-4)
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[.](#2.sentence-5)
Otherwise, if an exception is thrown by the move constructor of a non-*Cpp17CopyInsertable*T, the effects are unspecified[.](#2.sentence-6)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10358)
For the declarations taking a range R,
performs at most one reallocation if:
- [(3.1)](#3.1)
R models ranges::[approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3Approximately sized ranges[range.approximately.sized]") andranges::distance(rg) <= ranges::reserve_hint(rg) is true, or
- [(3.2)](#3.2)
R models ranges::[forward_range](range.refinements#concept:forward_range "25.4.6Other range refinements[range.refinements]") andR does not model ranges::[approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3Approximately sized ranges[range.approximately.sized]")[.](#3.sentence-1)
For the declarations taking a pair of InputIterator,
performs at most one reallocation ifInputIterator meets the *Cpp17ForwardIterator* requirements[.](#3.sentence-2)
[🔗](#lib:erase,vector)
`constexpr iterator erase(const_iterator position);
constexpr iterator erase(const_iterator first, const_iterator last);
constexpr void pop_back();
`
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10382)
*Effects*: Invalidates iterators and references at or after the point of the erase[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10386)
*Throws*: Nothing unless an exception is thrown by the
assignment operator or move assignment operator ofT[.](#5.sentence-1)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10392)
*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[.](#6.sentence-1)