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. Java垃圾回收理解

    gc是垃圾回收,Java的垃圾回收分为年轻代回收和老年代回收,其中年轻代回收速度快,频率高,因为Java对象大多具有朝生夕灭的特性,Java对象都是new出来的,当new出很多对象的时候,年轻代很容易 ...

  2. DevExpress WPF v18.2新版亮点(五)

    买 DevExpress Universal Subscription  免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...

  3. Centos7部署kubelet(六)

    1.二进制包准备将软件包从linux-node1复制linux-node2.linux-node3中去 [root@linux-node1 ssl]# cd /usr/local/src/kubern ...

  4. 程序包需要 NuGet 客户端版本“XXXXX”或更高版本,但当前的 NuGet 版本为“XXXXXXXXXX”

    工具 - 扩展和更新- visual studio 库

  5. 2019-03-11-day009-函数定义

    什么是函数 函数就是将许多冗余的代码进行整合统一调用的内存地址 函数怎么定义 def make(): print('掏出手机') print('打开微信') print('摇一摇') print('聊 ...

  6. spring — jdbc 配置文件的设置

    ---参考配置,  链接mysql 数据库 <!-- 1.配置数据源 --><bean id="dataSource" class="org.sprin ...

  7. 第四十二课 KMP算法的应用

    思考: replace图解: 程序完善: DTString.h: #ifndef DTSTRING_H #define DTSTRING_H #include "Object.h" ...

  8. python中的list按照某一列进行排序的方法

    如题,python中的list着实很好用,我有如下一个list 可以看出list中的每一个元素是由字符串,两个新的list,以及一个float组成,现在想根据这最后一个float对这个list进行排序 ...

  9. 九度OJ-1112-导弹拦截-最长不增子序列

    题目1112:拦截导弹 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5218 解决:2603 题目描述: 某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统.但是这种导弹拦截系统有一个缺 ...

  10. 再回首 基本数据类型和 if语句

    一 变量:(使用变量是不能加引号,要不就变成字符串了) 变量的命名规则: 1.数字,字母,下划线组成. 2.变量不能是数字开头 3.区分大小写 4.不要使用中文或者拼音 5.要有相应的意义 6.不能使 ...