[insert.iterators] # 24 Iterators library [[iterators]](./#iterators) ## 24.5 Iterator adaptors [[predef.iterators]](predef.iterators#insert.iterators) ### 24.5.2 Insert iterators [insert.iterators] #### [24.5.2.1](#general) General [[insert.iterators.general]](insert.iterators.general) [1](#general-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3779) To make it possible to deal with insertion in the same way as writing into an array, a special kind of iterator adaptors, called[*insert iterators*](#def:insert_iterators), are provided in the library[.](#general-1.sentence-1) With regular iterator classes,while (first != last) *result++ = *first++; causes a range [first, last) to be copied into a range starting with result[.](#general-1.sentence-2) The same code withresult being an insert iterator will insert corresponding elements into the container[.](#general-1.sentence-3) This device allows all of the copying algorithms in the library to work in the[*insert mode*](#def:insert_mode) instead of the [*regular overwrite*](#def:regular_overwrite) mode[.](#general-1.sentence-4) [2](#general-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3798) An insert iterator is constructed from a container and possibly one of its iterators pointing to where insertion takes place if it is neither at the beginning nor at the end of the container[.](#general-2.sentence-1) Insert iterators meet the requirements of output iterators[.](#general-2.sentence-2) operator* returns the insert iterator itself[.](#general-2.sentence-3) The assignmentoperator=(const T& x) is defined on insert iterators to allow writing into them, it insertsx right before where the insert iterator is pointing[.](#general-2.sentence-4) In other words, an insert iterator is like a cursor pointing into the container where the insertion takes place[.](#general-2.sentence-5) back_insert_iterator inserts elements at the end of a container,front_insert_iterator inserts elements at the beginning of a container, andinsert_iterator inserts elements where the iterator points to in a container[.](#general-2.sentence-6) back_inserter,front_inserter, andinserter are three functions making the insert iterators out of a container[.](#general-2.sentence-7) #### [24.5.2.2](#back.insert.iterator) Class template back_insert_iterator [[back.insert.iterator]](back.insert.iterator) #### [24.5.2.2.1](#back.insert.iter.general) General [[back.insert.iter.general]](back.insert.iter.general) [🔗](#lib:back_insert_iterator) namespace std {templateclass back_insert_iterator {protected: Container* container; public:using iterator_category = output_iterator_tag; using value_type = void; using difference_type = ptrdiff_t; using pointer = void; using reference = void; using container_type = Container; constexpr explicit back_insert_iterator(Container& x); constexpr back_insert_iterator& operator=(const typename Container::value_type& value); constexpr back_insert_iterator& operator=(typename Container::value_type&& value); constexpr back_insert_iterator& operator*(); constexpr back_insert_iterator& operator++(); constexpr back_insert_iterator operator++(int); };} #### [24.5.2.2.2](#back.insert.iter.ops) Operations [[back.insert.iter.ops]](back.insert.iter.ops) [🔗](#lib:back_insert_iterator,constructor) `constexpr explicit back_insert_iterator(Container& x); ` [1](#back.insert.iter.ops-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3863) *Effects*: Initializescontainer with addressof(x)[.](#back.insert.iter.ops-1.sentence-1) [🔗](#lib:operator=,back_insert_iterator) `constexpr back_insert_iterator& operator=(const typename Container::value_type& value); ` [2](#back.insert.iter.ops-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3876) *Effects*: As if by: container->push_back(value); [3](#back.insert.iter.ops-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3880) *Returns*: *this[.](#back.insert.iter.ops-3.sentence-1) [🔗](#lib:operator=,back_insert_iterator_) `constexpr back_insert_iterator& operator=(typename Container::value_type&& value); ` [4](#back.insert.iter.ops-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3891) *Effects*: As if by: container->push_back(std​::​move(value)); [5](#back.insert.iter.ops-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3895) *Returns*: *this[.](#back.insert.iter.ops-5.sentence-1) [🔗](#lib:operator*,back_insert_iterator) `constexpr back_insert_iterator& operator*(); ` [6](#back.insert.iter.ops-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3906) *Returns*: *this[.](#back.insert.iter.ops-6.sentence-1) [🔗](#lib:operator++,back_insert_iterator) `constexpr back_insert_iterator& operator++(); constexpr back_insert_iterator operator++(int); ` [7](#back.insert.iter.ops-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3918) *Returns*: *this[.](#back.insert.iter.ops-7.sentence-1) #### [24.5.2.2.3](#back.inserter) back_inserter [[back.inserter]](back.inserter) [🔗](#lib:back_inserter) `template constexpr back_insert_iterator back_inserter(Container& x); ` [1](#back.inserter-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3932) *Returns*: back_insert_iterator(x)[.](#back.inserter-1.sentence-1) #### [24.5.2.3](#front.insert.iterator) Class template front_insert_iterator [[front.insert.iterator]](front.insert.iterator) #### [24.5.2.3.1](#front.insert.iter.general) General [[front.insert.iter.general]](front.insert.iter.general) [🔗](#lib:front_insert_iterator) namespace std {templateclass front_insert_iterator {protected: Container* container; public:using iterator_category = output_iterator_tag; using value_type = void; using difference_type = ptrdiff_t; using pointer = void; using reference = void; using container_type = Container; constexpr explicit front_insert_iterator(Container& x); constexpr front_insert_iterator& operator=(const typename Container::value_type& value); constexpr front_insert_iterator& operator=(typename Container::value_type&& value); constexpr front_insert_iterator& operator*(); constexpr front_insert_iterator& operator++(); constexpr front_insert_iterator operator++(int); };} #### [24.5.2.3.2](#front.insert.iter.ops) Operations [[front.insert.iter.ops]](front.insert.iter.ops) [🔗](#lib:front_insert_iterator,constructor) `constexpr explicit front_insert_iterator(Container& x); ` [1](#front.insert.iter.ops-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3976) *Effects*: Initializescontainer with addressof(x)[.](#front.insert.iter.ops-1.sentence-1) [🔗](#lib:operator=,front_insert_iterator) `constexpr front_insert_iterator& operator=(const typename Container::value_type& value); ` [2](#front.insert.iter.ops-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3989) *Effects*: As if by: container->push_front(value); [3](#front.insert.iter.ops-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L3993) *Returns*: *this[.](#front.insert.iter.ops-3.sentence-1) [🔗](#lib:operator=,front_insert_iterator_) `constexpr front_insert_iterator& operator=(typename Container::value_type&& value); ` [4](#front.insert.iter.ops-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4004) *Effects*: As if by: container->push_front(std​::​move(value)); [5](#front.insert.iter.ops-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4008) *Returns*: *this[.](#front.insert.iter.ops-5.sentence-1) [🔗](#lib:operator*,front_insert_iterator) `constexpr front_insert_iterator& operator*(); ` [6](#front.insert.iter.ops-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4019) *Returns*: *this[.](#front.insert.iter.ops-6.sentence-1) [🔗](#lib:operator++,front_insert_iterator) `constexpr front_insert_iterator& operator++(); constexpr front_insert_iterator operator++(int); ` [7](#front.insert.iter.ops-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4031) *Returns*: *this[.](#front.insert.iter.ops-7.sentence-1) #### [24.5.2.3.3](#front.inserter) front_inserter [[front.inserter]](front.inserter) [🔗](#lib:front_inserter) `template constexpr front_insert_iterator front_inserter(Container& x); ` [1](#front.inserter-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4045) *Returns*: front_insert_iterator(x)[.](#front.inserter-1.sentence-1) #### [24.5.2.4](#insert.iterator) Class template insert_iterator [[insert.iterator]](insert.iterator) #### [24.5.2.4.1](#insert.iter.general) General [[insert.iter.general]](insert.iter.general) [🔗](#lib:insert_iterator) namespace std {templateclass insert_iterator {protected: Container* container; ranges::iterator_t iter; public:using iterator_category = output_iterator_tag; using value_type = void; using difference_type = ptrdiff_t; using pointer = void; using reference = void; using container_type = Container; constexpr insert_iterator(Container& x, ranges::iterator_t i); constexpr insert_iterator& operator=(const typename Container::value_type& value); constexpr insert_iterator& operator=(typename Container::value_type&& value); constexpr insert_iterator& operator*(); constexpr insert_iterator& operator++(); constexpr insert_iterator& operator++(int); };} #### [24.5.2.4.2](#insert.iter.ops) Operations [[insert.iter.ops]](insert.iter.ops) [🔗](#lib:insert_iterator,constructor) `constexpr insert_iterator(Container& x, ranges::iterator_t i); ` [1](#insert.iter.ops-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4090) *Effects*: Initializescontainer with addressof(x) anditer with i[.](#insert.iter.ops-1.sentence-1) [🔗](#lib:operator=,insert_iterator) `constexpr insert_iterator& operator=(const typename Container::value_type& value); ` [2](#insert.iter.ops-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4105) *Effects*: As if by:iter = container->insert(iter, value);++iter; [3](#insert.iter.ops-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4113) *Returns*: *this[.](#insert.iter.ops-3.sentence-1) [🔗](#lib:operator=,insert_iterator_) `constexpr insert_iterator& operator=(typename Container::value_type&& value); ` [4](#insert.iter.ops-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4124) *Effects*: As if by:iter = container->insert(iter, std::move(value));++iter; [5](#insert.iter.ops-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4132) *Returns*: *this[.](#insert.iter.ops-5.sentence-1) [🔗](#lib:operator*,insert_iterator) `constexpr insert_iterator& operator*(); ` [6](#insert.iter.ops-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4143) *Returns*: *this[.](#insert.iter.ops-6.sentence-1) [🔗](#lib:operator++,insert_iterator) `constexpr insert_iterator& operator++(); constexpr insert_iterator& operator++(int); ` [7](#insert.iter.ops-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4155) *Returns*: *this[.](#insert.iter.ops-7.sentence-1) #### [24.5.2.4.3](#inserter) inserter [[inserter]](inserter) [🔗](#lib:inserter) `template constexpr insert_iterator inserter(Container& x, ranges::iterator_t i); ` [1](#inserter-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iterators.tex#L4170) *Returns*: insert_iterator(x, i)[.](#inserter-1.sentence-1)