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. js 冒泡事件阻止 父层事件影响子层

    当父层 与子层 有相同的事件时,但子层跟父层执行的内容却不一样时 为了 防止 父层事件对子层造成影响我们可以在子层的方法里做如下操作 function A (event){ event.stopPro ...

  2. BeanUtils出现Java.lang.NoClassDefFoundError解决

    问题描述: javaWeb项目中导入了BeanUtils的两个包,但是还是出现Java.lang.NoClassDefFoundError: org/apache/commons/beanutils/ ...

  3. 牛客国庆集训派对Day5 数论之神

    题目描述 终于活成了自己讨厌的样子. 这是她们都还没长大的时候发生的故事.那个时候,栗子米也不需要为了所谓的爱情苦恼. 她们可以在夏日的午后,花大把的时间去研究生活中一些琐碎而有趣的事情,比如数论. ...

  4. MYSQL锁表问题解决

    本文实例讲述了MYSQL锁表问题的解决方法.分享给大家供大家参考,具体如下: 很多时候!一不小心就锁表!这里讲解决锁表终极方法! 案例一 ? 1 mysql>show processlist; ...

  5. 登陆网页模板 - 1 (HTML+CSS)

    一个用HTML和CSS写的简单登录页面,主要是用CSS来进行修饰美化的 这个登陆界面有输入账号和密码的表单,还有登陆和注册两个按键,点击按键分别会输出“您已成功登陆,稍后会跳转到您需要的页面~”,“您 ...

  6. java小程序-----用if for写会员登陆和商品列表

    一.父类 public class Father{ //父类 protected static int stock1 = 10; //库存 protected static int stock2 = ...

  7. 找出n个自然数(1,2,3,……,n)中取r个数的组合

    <?php /** * 对于$n和$r比较小, 可以用这种方法(当n=5, r=3时) */ function permutation1($n, $r) { for($i=1; $i<=$ ...

  8. Gauss error function

    0. error function erf(x)=1π∫−xxe−t2dt" role="presentation">erf(x)=1π−−√∫x−xe−t2dte ...

  9. python代理可用检测、代理类型检测

    #coding:utf-8 import urllib2 def url_user_agent(proxy,url): proxy_support = urllib2.ProxyHandler({'h ...

  10. zedboard开发板上移植opencv代码(立体匹配)

    前言 公司要做立体匹配相关的项目,已有matlab和c++版本,可是不能做到实时显示立体信息,想要硬件实现实时,无奈本渣也是个硬件的新手,先按照实验室lyq同学的思路在zedboard开发板的纯ARM ...