C++ STL算法:复制和删除操作

C++ STL算法:复制和删除操作

STL 提供了三个重要的复制函数: copy( )、 copy_if( )和 copy_backward( )。 copy 沿向前的方向将源
范围的内容赋给目标范围:

auto lastElement = copy (numsInList.cbegin(), // start source range
numsInList.cend(), // end source range
numsInVec.begin()); // start dest range  

copy_if( )是 C++11 新增的,仅在指定的一元谓词返回 true 时才复制元素:

// copy odd numbers from list into vector
copy_if (numsInList.cbegin(), numsInList.cend(),
lastElement, // copy position in dest range
[](int element){return ((element % 2) == 1);});

copy_backward( )沿向后的方向将源范围的内容赋给目标范围:

copy_backward (numsInList.cbegin (),
numsInList.cend (),
numsInVec.end ());

remove( )将容器中与指定值匹配的元素删除:

// Remove all instances of '0', resize vector using erase()
auto newEnd = remove (numsInVec.begin (), numsInVec.end (), 0);
numsInVec.erase (newEnd, numsInVec.end ());

remove_if( )使用一个一元谓词,并将容器中满足该谓词的元素删除:</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值