remove

  因为本算法作用的是iterator,所以并不会改变Container大小,会返回一个新的iterator new_last,是的first到new_last中的元素都不等于value,左端元素的相对位置不变

template <class ForwardIterator,class T>
ForwardIterator remove(ForwardIterator first,ForwardIterator last,const T& value);

remove_if

  移除pred(*i)为true的元素

template <class ForwardIterator,class Predicate>
ForwardIterator remove_if(ForwardIterator first,ForwardIterator last,Predicate pred);

remove_copy

  不会复制值与value相等元素,返回值是result的尾端,被复制的元素的相对位置不改变

template <class ForwardIterator,class OutputIterator,class T>
ForwardIterator remove_copy(ForwardIterator first,ForwardIterator last,OutputIterator result,const T& value);

remove_copy_if

template <class ForwardIterator,class OutputIterator,class Predicate>
ForwardIterator remove_copy_if(ForwardIterator first,ForwardIterator last,OutputIterator result,Predicate pred);

unique

  元素去重。即”删除”序列中所有相邻的重复元素(只保留一个)。并不是真的删除,而是指重复元素的位置被不重复的元素给占领。把不重复的元素移到前面来,未被移除的元素的相对位置不改变。

  返回值是一个迭代器,它指向的是去重后容器中不重复序列的最后一个元素的下一个元素

//版本一:重载operator==,如果*i==*(i-1)则有重复的元素
template <class ForwardIterator>
ForwardIterator unique(ForwardIterator fisrt,ForwardIterator last); //版本二:自定义函数对象cmp(*i,*(i-1))为true,有重复
template <class ForwardIterator,class BinaryPredicate>
ForwardIterator unique(ForwardIterator fisrt,ForwardIterator last,BinaryPredicate cmp);

unique_copy

//版本一:重载operator==,如果*i==*(i-1)则有重复的元素
template <class InputIterator,class OutputIterator>
OutputIterator unique_copy(ForwardIterator fisrt,ForwardIterator last,OutputIterator result); //版本二:自定义函数对象cmp(*i,*(i-1))为true,有重复
template <class ForwardIterator,class OutputIterator,class BinaryPredicate>
OutputIterator unique_copy(ForwardIterator fisrt,ForwardIterator last,OutputIterator result,BinaryPredicate cmp);
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
#include <iterator>
using namespace std; class F
{
public:
bool operator()(int i)
{
return i&==;
}
};
class F1
{
public:
F1(string t):s1(t){}
bool operator()(string s)
{
return !strcmp(s.c_str(),s1.c_str());
}
private:
string s1;
};
int main()
{
vector<int> v{,,,,,};
//auto it=remove_if(v.begin(),v.end(),F());
//cout<<*--it<<endl;
//v.erase(remove_if(v.begin(),v.end(),F()),v.end()); vector<string> vs{"hello"," ","word!","how"," ","you","."};
remove_copy_if(vs.begin(),vs.end(),ostream_iterator<string>(cout," "),F1("you"));
/*for(auto i:vs)
cout<<i<<' ';
cout<<endl;*/
return ;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class F
{
public:
F(vector<int> t):_v(t){}
bool operator()(int i,int j)
{
if(count(_v.begin(),_v.end(),i)!=)
return true;
else
return false;
}
private:
vector<int> _v;
};
int main()
{
//用sort,unique函数去除相邻重复的元素
vector<int> arr{,,,,,,,};
sort(arr.begin(),arr.end(),greater<int>());
arr.erase(unique(arr.begin(),arr.end()),arr.end());//unique返回new_last,其中不包含重复的元素
for_each(arr.begin(),arr.end(),[](int i)
{
cout<<i<<' ';
});
cout<<endl; vector<int> arr1{,,,,,,,,};
arr1.erase(unique(arr1.begin(),arr1.end(),F(arr1)),arr1.end());
for_each(begin(arr1),end(arr1),[](int i)
{
cout<<i<<' ';
});
cout<<endl;
return ; }

移除元素(remove,remove_if...unique...)的更多相关文章

  1. [Swift]LeetCode27. 移除元素 | Remove Element

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  2. LeetcCode 27:移除元素 Remove Element(python、java)

    公众号:爱写bug 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) ...

  3. 【LeetCode】移除元素(Remove Element)

    这道题是LeetCode里的第27道题. 题目描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...

  4. list源码3(参考STL源码--侯捷):push_front、push_back、erase、pop_front、pop_back、clear、remove、unique

    list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...

  5. 分析轮子(八)- List.java 各种遍历方式及遍历时移除元素的方法

    注:玩的是JDK1.7版本 1:先尝栗子,再分析,代码简单,注释清晰,可自玩一下 /** * @description:测试集合遍历和移除元素的方式 * @author:godtrue * @crea ...

  6. lua中table如何安全移除元素

    在Lua中,table如何安全的移除元素这点挺重要,因为如果不小心,会没有正确的移除,造成内存泄漏. 引子 比如有些朋友常常这么做,大家看有啥问题 将test表中的偶数移除掉local test = ...

  7. Java易错知识点(1) - 关于ArrayList移除元素后剩下的元素会立即重排

    帮一个网友解答问题时,发现这样一个易错知识点,现总结如下: 1.易错点: ArrayList移除元素后,剩下的元素会立即重排,他的 size() 也会立即减小,在循环过程中容易出错.(拓展:延伸到所有 ...

  8. Leecode刷题之旅-C语言/python-26.移除元素

    /* * @lc app=leetcode.cn id=27 lang=c * * [27] 移除元素 * * https://leetcode-cn.com/problems/remove-elem ...

  9. 前端与算法 leetcode 27.移除元素

    目录 # 前端与算法 leetcode 27.移除元素 题目描述 概要 提示 解析 算法 @(目录) # 前端与算法 leetcode 27.移除元素 题目描述 27.移除元素 概要 题目本身其实挺简 ...

随机推荐

  1. Iterator 与ListIterator的区别

    Iterator 与ListIterator的区别: 1.Iterator能够迭代Set和List集合的元素,而ListIterator只能迭代List集合的元素 2.Iterator只能前向迭代,L ...

  2. bootstrap table 分页显示问题

    1.bootstrap-table客户端分页 客户端分页的数据源可以是后台服务器端传递过来(一次性获取,即获取所有你需要的数据),点击页码不再请求后台,利用页面缓存分页;cache : true, / ...

  3. L260

    Innovative UK technology that can deliver drugs deep into the brain to treat neurological diseases, ...

  4. restful接口设计规范总结

    这篇 文章主要是借鉴他人,但是自己很想总结出一套规范,以供向我这样的新手使用,用来规范代码,如果有什么好的提议,请不吝赐教,本篇文章长期更新! 一.重要概念: REST,即Representation ...

  5. xampp 修改 时区为中国. timezone

    注意: xampp中的timezone 总共有两处. 第一处: 是 php.ini 默认的配置项. 第二处:是xampp在 php.ini 中重写. 之前我 只修改了 941行的代码, 结果 2013 ...

  6. BZOJ 1083 [SCOI2005]繁忙的都市 (最小生成树裸题无重边) 超简单写法!!

    Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口 ...

  7. 【Python】socket编程-3

    . SocketServer最简单的使用方法: () 创建一个Handler类,继承自BaseRequestHandler,重写其handle(),在该方法中完成对请求的处理. () 实例化一个Ser ...

  8. 正则表达式的lastIndex属性

    js中正则表达式的使用方式有两种,一种是正则表达式对象的方法,一种是字符串对象的方法,前者有exec(str).test(str)两个方法,后者有match(regexp).replace(regex ...

  9. JavaScript中的内置对象-8--4.date对象中-获取,设置日期时间的方法; 获取,设置年月日时分秒及星期的方法;

    学习目标 1.掌握创建日期对象的方法 2.掌握date对象中获取日期时间的方法 3.掌握date对象中设置日期时间的方法 如何创建一个日期对象 语法:new Date(); 功能:创建一个日期时间对象 ...

  10. Ubuntu 18.04开启TCP网络协议BBR加速的方法(Google BBR 拥塞控制算法)

    TCP BBR 是Google给出的一个改良版的tcp网络协议,相当于在已有TCP协议的基础上打了个补丁的意思,这个改良版TCP协议对拥塞控制有很好的支持,对于网络较差的环境有不错的应用场景,当然这里 ...