Adaptors
Iterators can do more than just search through data, they can now insert values!
We'll cover the following
Iterator adaptors enable the use of iterators in insert mode or with streams. They need the header <iterator>
.
Insert iterators
With the three insert iterators std::front_inserter
, std::back_inserter
, and std::inserter
, we can insert an element into a container at the beginning, at the end or an arbitrary position respectively. The memory for the elements will be automatically provided. All of these insert iterators map their functionality on the underlying methods of the container cont
.
The table below gives us two pieces of information: Which methods of the containers are internally used and which iterators can be used depending on the container’s type.
Name | Internally-used Method | Container |
---|---|---|
std::front_inserter(val) |
cont.push_front(val) |
std::deque |
std::list |
||
std::back_inserter(val) |
cont.push_back(val) |
std::vector |
std::deque |
||
std::list |
||
std::string |
||
std::inserter(val, pos) |
cont.insert(pos, val) |
std::vector |
std::deque |
||
std::list |
||
std::string |
||
std::map |
||
std::set |
The three insert iterators
We can combine the algorithms in the STL with the three insert iterators.
Get hands-on with 1400+ tech skills courses.