移除元素(remove,remove_if...unique...)
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...)的更多相关文章
- [Swift]LeetCode27. 移除元素 | Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- LeetcCode 27:移除元素 Remove Element(python、java)
公众号:爱写bug 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) ...
- 【LeetCode】移除元素(Remove Element)
这道题是LeetCode里的第27道题. 题目描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
- 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 ...
- 分析轮子(八)- List.java 各种遍历方式及遍历时移除元素的方法
注:玩的是JDK1.7版本 1:先尝栗子,再分析,代码简单,注释清晰,可自玩一下 /** * @description:测试集合遍历和移除元素的方式 * @author:godtrue * @crea ...
- lua中table如何安全移除元素
在Lua中,table如何安全的移除元素这点挺重要,因为如果不小心,会没有正确的移除,造成内存泄漏. 引子 比如有些朋友常常这么做,大家看有啥问题 将test表中的偶数移除掉local test = ...
- Java易错知识点(1) - 关于ArrayList移除元素后剩下的元素会立即重排
帮一个网友解答问题时,发现这样一个易错知识点,现总结如下: 1.易错点: ArrayList移除元素后,剩下的元素会立即重排,他的 size() 也会立即减小,在循环过程中容易出错.(拓展:延伸到所有 ...
- Leecode刷题之旅-C语言/python-26.移除元素
/* * @lc app=leetcode.cn id=27 lang=c * * [27] 移除元素 * * https://leetcode-cn.com/problems/remove-elem ...
- 前端与算法 leetcode 27.移除元素
目录 # 前端与算法 leetcode 27.移除元素 题目描述 概要 提示 解析 算法 @(目录) # 前端与算法 leetcode 27.移除元素 题目描述 27.移除元素 概要 题目本身其实挺简 ...
随机推荐
- 区分舍入函数fix/round/ceil/floor
1)fix(n)的意义是取小于n的整数(是向零点舍入的意思是往零的方向上靠),这是一类应用在整数取值上的函数,就如同以前我们所研究的求整问题: 例如:fix(pi)=3 ; fix(3.5)=3; ...
- Docker(2):快速入门及常用命令
什么是Docker? Docker 是世界领先的软件容器平台.开发人员利用 Docker 可以消除协作编码时“在我的机器上可正常工作”的问题.运维人员利用 Docker 可以在隔离容器中并行运行和管理 ...
- [转] ajax方法
1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...
- alter system archive log current作用及和alter system switch logfile区别
alter system archive log current 是归档当前的重做日志文件,不管自动归档有没有打都归档. alter system switch logfile 是强制日志切换,不一定 ...
- 2019-04-02-day024-内置方法
昨日回顾 反射 用"字符串"类型的属性名/方法名来找到 属性的值或者方法的内存地址 所有可以反射的内容实际上都是变量 有内存地址 内存地址存的是"具体的值",直 ...
- [Mac]ssh免密登陆配置
在已经有公钥和私钥的情况下,只需要以下三步即可实现免密登陆: 1.将已有rsa公钥和私钥拷贝到~/.ssh目录下. 2.编辑配置文件:vim ~/.ssh/config,内容如下: Host xxx ...
- 性能测试-8.LR常用函数
1.变量转参数 lr_save_string("参数内容","param"):将字符串“aaa”或者一个字符串变量,转变成LR的参数{param} 2.参数转变 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - 数据访问与多数据源配置
文章目录 1. 环境依赖 2. 数据源 3. 单元测试 4. 源代码 在某些场景下,我们可能会在一个应用中需要依赖和访问多个数据源,例如针对于 MySQL 的分库场景.因此,我们需要配置多个数据源. ...
- 第四十一课 KMP子串查找算法
问题: 右移的位数和目标串没有多大的关系,和子串有关系. 已匹配的字符数现在已经有了,部分匹配值还没有. 前六位匹配成功就去查找PMT中的第六位. 现在的任务就是求得部分匹配表. 问题:怎么得到部分匹 ...
- reids的搭建
---恢复内容开始--- redis的安装 源码包安装 以reids3.0为例 先安装编译的软件 gcc gcc-c++ make yum -y install gcc gcc-c++ make ...