Move inserters

| Home | Libraries | People | FAQ | More

## Move inserters

Similar to standard insert iterators, it's possible to deal with move insertion in the same way as writing into an array. A special kind of iterator adaptors, called move insert iterators, are provided with this 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 with result being a move insert iterator will move insert corresponding elements into the container. This device allows all of the copying algorithms in the library to work in the move insert mode instead of the regular overwrite mode. This library offers 3 move insert iterators and their helper functions:

    // Note: C models Container
    template <typename C>
    class back_move_insert_iterator;

    template <typename C>
    back_move_insert_iterator<C> back_move_inserter(C& x);

    template <typename C>
    class front_move_insert_iterator;

    template <typename C>
    front_move_insert_iterator<C> front_move_inserter(C& x);

    template <typename C>
    class move_insert_iterator;

    template <typename C>
    move_insert_iterator<C> move_inserter(C& x, typename C::iterator it);

A move 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 satisfy the requirements of output iterators. `operator*` returns the move insert iterator itself. The assignment `operator=(T& x)` is defined on insert iterators to allow writing into them, it inserts x 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_move_iterator` move inserts elements at the end of a container, `front_insert_iterator` move inserts elements at the beginning of a container, and `move_insert_iterator` move inserts elements where the iterator points to in a container. `back_move_inserter`, `front_move_inserter`, and `move_inserter` are three functions making the insert iterators out of a container. Here's an example of how to use them:

    #include <boost/container/list.hpp>
    #include "movable.hpp"
    #include <cassert>
    #include <algorithm>

    using namespace ::boost::container;

    typedef list<movable> list_t;
    typedef list_t::iterator l_iterator;

    template<class MoveInsertIterator>
    void test_move_inserter(list_t &l2, MoveInsertIterator mit)
    {
       //Create a list with 10 default constructed objects
       list<movable> l(10);
       assert(!l.begin()->moved());
       l2.clear();

       //Move insert into l2 containers
       std::copy(l.begin(), l.end(), mit);

       //Check size and status
       assert(l2.size() == l.size());
       assert(l.begin()->moved());
       assert(!l2.begin()->moved());
    }

    int main()
    {
       list_t l2;
       test_move_inserter(l2, boost::back_move_inserter(l2));
       test_move_inserter(l2, boost::front_move_inserter(l2));
       test_move_inserter(l2, boost::move_inserter(l2, l2.end()));
       return 0;
    }