iterator adaptors - insert iterators

来源:互联网 发布:hive数据导入 编辑:程序博客网 时间:2024/06/15 20:40

Three different insert iterators

The trick Operations:


Implementations of insert iterators usually use the following two-step trick:
1) Operator * is implemented as a no-op that simply returns *this. Thus, for insert iterators, *pos is equivalent to pos.
2) The assignment operator is implemented so that it gets transferred into an insertion. In fact, the insert iterator calls the push_back(), push_front(), or insert() member function of the container.

Since each inserter uses a different member function, which it calls for the container to which it belongs, an insert iterator must be always initialized with its container.
vector<int> coll;
back_insert_iterator<decltype(coll)> iter(coll);
*iter = 1;  // insert
*iter = 2;  // insert

set<int> coll2;

insert_iterator<decltype(coll1)> iter2(coll2, coll2.begin());
*iter = 1;
*iter = 2;
*iter = 3;
inserter(coll2,coll2.end()) = 44;

list<int> coll3;
copy (coll2.begin(), col2l.end(), // source
           inserter(coll3,coll3.begin())); // destination

原创粉丝点击