移除元素(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.移除元素 概要 题目本身其实挺简 ...
随机推荐
- CentOS7安装配置Amanda
参考: https://wenku.baidu.com/view/881e0c998e9951e79a892759.html yum -y install amanda* http://blog ...
- Android开发---如何操作资源目录中的资源文件3--圆角边框、背景颜色渐变效果、边框颜色
Android开发---如何操作资源目录中的资源文件3 效果图 1.圆角边框 2.背景颜色渐变效果 1.activity_main.xml 描述: 定义了一个shape资源管理按钮 <?xml ...
- 90%会搞错的JavaScript闭包问题
由工作中演变而来的面试题 这是一个我工作当中的遇到的一个问题,似乎很有趣,就当做了一道题去面试,发现几乎没人能全部答对并说出原因,遂拿出来聊一聊吧. 先看题目代码: function fun(n,o) ...
- L248 词汇题 2006
The audience, hostile at first, were greatly impressed by her excellent performance. He wanted to st ...
- Delphi 10.3.1 TNetHttpClient在多线程中存在的问题及解决方法。
Delphi 10.3.1发布了,对10.3.0存在的各种问题,做了大量的修正.但听高勇说TNetHttpClient在多线程中存在问题,今天做了一下测试,确实如此,看来,还需要官方进一步修正! 具体 ...
- Python 爬虫的工具列表大全
Python 爬虫的工具列表大全 这个列表包含与网页抓取和数据处理的Python库.网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pyc ...
- 爬虫框架存储pymysql方式
爬虫框架存储pymysql方式# -*- coding: utf-8 -*-import pymysql# Define your item pipelines here## Don't forget ...
- swift简单处理调用高清大图导致内存暴涨的情况
开发中,通常需要用到使用选取多张图片的功能,但是高清大图很吃内存,我想到的处理方案就是拿到高清大图的时候,重新绘制一张小的图片使用.至于清晰度尚可,至少我是分辨不出多大区别. 基本思路就是先固定宽,然 ...
- grep命令相关用法
grep命令相关参数: -i:忽略大小写 --color:高亮显示匹配到的信息 -v:反向查找,没匹配到的行显示出来 -o:只显示被模式匹配到的串本身 正则表达式: .*:任意长度的任意字符,贪婪模式 ...
- PHP安全之Web攻击(转)
一.SQL注入攻击(SQL Injection) 攻击者把SQL命令插入到Web表单的输入域或页面请求的字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响)动态 ...