iterator adaptors - insert iterators

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值