STL 去重 unique
一.unique函数
类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素。
该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值范围得结束。
- 1 // sort words alphabetically so we can find the duplicates
- 2 sort(words.begin(), words.end());
- 3 /* eliminate duplicate words:
- 4 * unique reorders words so that each word appears once in the
- 5 * front portion of words and returns an iterator one past the
- 6 unique range;
- 7 * erase uses a vector operation to remove the nonunique elements
- 8 */
- 9 vector<string>::iterator end_unique = unique(words.begin(), words.end());
- 10 words.erase(end_unique, words.end());
在STL中unique函数是一个去重函数, unique的功能是去除相邻的重复元素(只保留一个),其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,然后 返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
若调用sort后,vector的对象的元素按次序排列如下:
- sort jumps over quick red red slow the the turtle
则调用unique后,vector中存储的内容是:
注意,words的大小并没有改变,依然保存着10个元素;只是这些元素的顺序改变了。调用unique“删除”了相邻的重复值。给“删除”加上引号是因为unique实际上并没有删除任何元素,而是将无重复的元素复制到序列的前段,从而覆盖相邻的重复元素。unique返回的迭代器指向超出无重复的元素范围末端的下一个位置。
注意:算法不直接修改容器的大小。如果需要添加或删除元素,则必须使用容器操作。
- 1 #include <iostream>
- 2 #include <cassert>
- 3 #include <algorithm>
- 4 #include <vector>
- 5 #include <string>
- 6 #include <iterator>
- 7 using namespace std;
- 8
- 9 int main()
- 10 {
- 11 //cout<<"Illustrating the generic unique algorithm."<<endl;
- 12 const int N=11;
- 13 int array1[N]={1,2,0,3,3,0,7,7,7,0,8};
- 14 vector<int> vector1;
- 15 for (int i=0;i<N;++i)
- 16 vector1.push_back(array1[i]);
- 17
- 18 vector<int>::iterator new_end;
- 19 new_end=unique(vector1.begin(),vector1.end()); //"删除"相邻的重复元素
- 20 assert(vector1.size()==N);
- 21
- 22 vector1.erase(new_end,vector1.end()); //删除(真正的删除)重复的元素
- 23 copy(vector1.begin(),vector1.end(),ostream_iterator<int>(cout," "));
- 24 cout<<endl;
- 25
- 26 return 0;
- 27 }
STL 去重 unique的更多相关文章
- javascript 数组去重 unique
晚上无事,偶然看到这么个小测试,拿来写一写,希望大家提建议: 直接上代码: Array.prototype.unique = function (isStrict) { if (this.length ...
- STL中unique的使用
作用 unique函数可以删除有序数组中的重复元素,即去重(并不是真正的删除,后面会讲) 定义在头文件<algorithm>中 函数原型 1.只有两个参数,且参数类型都是迭代器: iter ...
- STL:unique()函数
unique() unique()是剔除重复他是剔除相邻之间字符重复的,倘若其中中的字符前后之间是没有重复的,unique函数是起不到作用的,所以使用以前都会sort处理. unique()函数的返回 ...
- vector去重--unique
具体实现见中间源码 function template <algorithm> std::unique equality (1) template <class ForwardIte ...
- C++ STL算法系列 unique
类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...
- unique() 去重函数
unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个), 还有一个容易忽视的特性是它并不真正把重复的元素删除.他是c++中的函数, 所以头 ...
- unique函数 (STL)
转自http://www.cnblogs.com/heyonggang/archive/2013/08/07/3243477.html 类属性算法unique的作用是从输入序列中“删除”所有相邻的重复 ...
- 西安电子科技大学第16届程序设计竞赛 F Operating System (unique() 去重函数)
链接:https://www.nowcoder.com/acm/contest/107/F来源:牛客网 Operating System 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ ...
- unique函数(STL)
unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除.他是c++中的函数,所以头文件 ...
随机推荐
- 关于php的ini文件相关操作函数浅析
在小公司,特别是创业型公司,整个服务器的搭建一般也是我们 PHP 开发工程师的职责之一.其中,最主要的一项就是要配置好服务器的 php.ini 文件.一些参数会对服务器的性能产生深远的影响,而且也有些 ...
- webpack learn1-配置项目加载各种静态资源及css预处理器2
继续在webpack.config.js中配置loader { test:/\.css$/, use: [ 'style-loader', 'css-loader' ] },{ test:/\.(jp ...
- Jmeter扩展组件开发(8) - 函数助手扩展开发demo
前提条件 1.pom文件引用ApacheJMeter_functions包 <dependency> <groupId>org.apache.jmeter</groupI ...
- javascript wchar_t 宽字符 转化为 ascii字符码数组
String.prototype.charCodeAt String.fromCharCode() String.prototype.toUtfArray = function() { return ...
- command ' cl.exe' failed: No such file or directory解决办法
1.安装C ++编译器 https://pan.baidu.com/s/1D1-tM-mWO4TVLdTrh3k1GA 提取码:ym67 2.找到安装文件夹:Visual C++ Build T ...
- python-requests包请求响应时间
p.p1 { margin: 0; font: 14px "Helvetica Neue"; color: rgba(17, 17, 17, 1) } p.p2 { margin: ...
- CF204E-Little Elephant and Strings【广义SAM,线段树合并】
正题 题目链接:https://www.luogu.com.cn/problem/CF204E 题目大意 \(n\)个字符串的一个字符串集合,对于每个字符串求有多少个子串是这个字符串集合中至少\(k\ ...
- P5644-[PKUWC2018]猎人杀【NTT,分治】
正题 题目链接:https://www.luogu.com.cn/problem/P5644 题目大意 \(n\)个人,每个人被选中的权重是\(a_i\).每次按照权重选择一个没有死掉的人杀死,求第\ ...
- CF605E-Intergalaxy Trips【期望dp】
正题 题目链接:https://www.luogu.com.cn/problem/CF605E 题目大意 给出\(n\)个点的一张完全有向图,每一天\(i\)到\(j\)的路径有\(p_{i,j}\) ...
- FastAPI(38)- 模拟一个跨域场景
同源策略 https://www.cnblogs.com/poloyy/p/15345184.html CORS https://www.cnblogs.com/poloyy/p/15345871.h ...