STL学习笔记----12.STL算法之 (移除性算法)

一. 概述

移除性算法是在一区间内移除某些元素,这些算法并不能改变元素的数量,它们只是以逻辑上的思考,将原本置于后面的“不移除元素”向前移动,覆盖那些被移除元素而已。它们都返回新区间的逻辑终点。

remove()将等于某特定值的元素全部移除
remove_if()将满足某准则的元素全部移除
remove_copy()将不等于某特定值的元素全部复制到它处
remove_copy()_if()将不满足某准则的元素全部复制到它处
unique()移除毗邻的重复元素
unique_copy()移除毗邻的重复元素,并复制到它处
二. 移除某些特定元素

1. 移除某序列内的元素

  1. //移除区间[beg, end)中每一个与value相等的元素 
  2. ForwardIterator 
  3. remove (ForwardIterator beg, ForwardIterator end,  
  4.         const T& value) 
  5.  
  6. //移除区间[beg, end)中每一个令op(elem)为true的元素 
  7. ForwardIterator 
  8. remove_if (ForwardIterator beg, ForwardIterator end,  
  9.            UnaryPredicate op) 
//移除区间[beg, end)中每一个与value相等的元素
ForwardIterator
remove (ForwardIterator beg, ForwardIterator end, 
        const T& value)

//移除区间[beg, end)中每一个令op(elem)为true的元素
ForwardIterator
remove_if (ForwardIterator beg, ForwardIterator end, 
           UnaryPredicate op)

2. 复制时一并移除元素
  1. //将源区间[beg, end)内所有元素复制到以destBeg为起点的目标区间去, 
  2. //并在复制过程中移除与value相等的所有元素 
  3. OutputIterator 
  4. remove_copy (InputIterator sourceBeg, InputIterator sourceEnd,  
  5.              OutputIterator destBeg,  
  6.              const T& value) 
  7.  
  8. //两个算法都返回目标区间中最后一个被复制元素的下一位置 
  9. OutputIterator 
  10. remove_copy_if (InputIterator sourceBeg, InputIterator sourceEnd,  
  11.                 OutputIterator destBeg,  
  12.                 UnaryPredicate op) 
//将源区间[beg, end)内所有元素复制到以destBeg为起点的目标区间去,
//并在复制过程中移除与value相等的所有元素
OutputIterator
remove_copy (InputIterator sourceBeg, InputIterator sourceEnd, 
             OutputIterator destBeg, 
             const T& value)

//两个算法都返回目标区间中最后一个被复制元素的下一位置
OutputIterator
remove_copy_if (InputIterator sourceBeg, InputIterator sourceEnd, 
                OutputIterator destBeg, 
                UnaryPredicate op)
三. 移除重复元素

1. 移除连续重复元素

  1. //移除连续重复的元素,源序列必须是已排序的 
  2. void 
  3. unique (ForwardIterator beg, ForwardIterator end) 
  4.  
  5. void 
  6. unique (Forwardlterator beg, ForwardIterator end,  
  7.         BinaryPredicate op) 
//移除连续重复的元素,源序列必须是已排序的
void
unique (ForwardIterator beg, ForwardIterator end)

void
unique (Forwardlterator beg, ForwardIterator end, 
        BinaryPredicate op)
2. 复制过程中移除重复元素
  1. //将源区间[sourceBeg, sourceEnd)内的元素复制到以destBeg起始的目标区间 
  2. //并移除重复元素 
  3. OutputIterator 
  4. unique_copy (InputIterator sourceBeg, InputIterator sourceEnd,  
  5.              OutputIterator destBeg) 
  6.  
  7. //两个算法都返回目标区间内最后一个被复制的元素的下一个位置 
  8. OutputIterator 
  9. unique_copy (InputIterator sourceBeg, InputIterator sourceEnd,  
  10.              OutputIterator destBeg,  
  11.              BinaryPredicate op) 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值