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
本文介绍了三种不同的插入迭代器及其常用操作技巧。通过两步法实现插入操作:首先,使用无操作符将迭代器自身返回;其次,通过赋值操作调用容器的成员函数如 push_back(), push_front() 或 insert() 来完成元素的插入。文章还提供了具体的代码示例来展示如何使用这些插入迭代器。
2万+

被折叠的 条评论
为什么被折叠?



