Three common iterator
generator: inserter(), back_inserter(), front_inserter(); They will
produces three insert_iterator: insert_iterator, back_insert_iterator
and front_insert_iterator.
When you use insert_iterator in an assignment, insert_iterator will
call insert() function. back_insert_iterator will call push_back() and
front_insert_iterator will call push_front(). insert_iterator++ has no
any operator inside.
std::inserter is commonly used with sets
Why do we need it. in some algorithm, such as copy and generator, if
you read sth from a container, you can use regular iterator, but when
you want to write into a container. You must keep regular iterator is
valid. so a method is to use reverse before you write to a container.
Of course, you can use the random access iterators (or any output iterator)
in algorithms like std::copy, as third argument, but that assumes the iterator
is referencing to existing range âĂŤ *it and ++it are well-defined for the
value you passed. You pass them to overwrite the existing elements of the
range, whereas std::back_insert_iterator adds new elements to the
container.
For copy algorithm, pass vect.end() is not meanful, because It’s not a valid
iterator to support *it = new_obj.