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

2.2 KiB

[insert.iterators.general]

24 Iterators library [iterators]

24.5 Iterator adaptors [predef.iterators]

24.5.2 Insert iterators [insert.iterators]

24.5.2.1 General [insert.iterators.general]

1

#

To make it possible to deal with insertion in the same way as writing into an array, a special kind of iterator adaptors, calledinsert iterators, are provided in the library.

With regular iterator classes,while (first != last) *result++ = *first++; causes a range [first, last) to be copied into a range starting with result.

The same code withresult being an insert iterator will insert corresponding elements into the container.

This device allows all of the copying algorithms in the library to work in theinsert mode instead of the regular overwrite mode.

2

#

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.

Insert iterators meet the requirements of output iterators.

operator* returns the insert iterator itself.

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.

In other words, an insert iterator is like a cursor pointing into the container where the insertion takes place.

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.

back_inserter,front_inserter, andinserter are three functions making the insert iterators out of a container.