[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 constexpr iterator insert(const_iterator position, InputIterator first, InputIterator last); template<[container-compatible-range](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]") R> constexpr iterator insert_range(const_iterator position, R&& rg); constexpr iterator insert(const_iterator position, initializer_list); template constexpr reference emplace_back(Args&&... args); template 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.1 Introduction [container.intro.reqmts]") 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 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.3 Approximately 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.6 Other range refinements [range.refinements]") andR does not model ranges​::​[approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately 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)