vector去重--unique
来源:互联网 发布:倍增lca算法 编辑:程序博客网 时间:2024/05/01 17:08
std::unique
template <class ForwardIterator> ForwardIterator unique (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique (ForwardIterator first, ForwardIterator last, BinaryPredicate pred);
[first,last)
.The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of an array or a container): The removal is done by replacing the duplicate elements by the next element that is not a duplicate, and signaling the new size of the shortened range by returning an iterator to the element that should be considered its new past-the-end element.
The relative order of the elements not removed is preserved, while the elements between the returned iterator and lastare left in a valid but unspecified state.
The function uses
operator==
to compare the pairs of elements (or pred, in version (2)).The behavior of this function template is equivalent to:
12345678910111213
template <class ForwardIterator> ForwardIterator unique (ForwardIterator first, ForwardIterator last){ if (first==last) return last; ForwardIterator result = first; while (++first != last) { if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2) *(++result)=*first; } return ++result;}
Parameters
- first, last
- Forward iterators to the initial and final positions of the sequence of move-assignable elements. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. - pred
- Binary function that accepts two elements in the range as argument, and returns a value convertible to
bool
. The value returned indicates whether both arguments are considered equivalent (iftrue
, they are equivalent and one of them is removed).
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Return value
An iterator to the element that follows the last element not removed.The range between first and this iterator includes all the elements in the sequence that were not considered duplicates.
Example
12345678910111213141516171819202122232425262728293031
// unique algorithm example#include <iostream> // std::cout#include <algorithm> // std::unique, std::distance#include <vector> // std::vectorbool myfunction (int i, int j) { return (i==j);}int main () { int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10 std::vector<int> myvector (myints,myints+9); // using default comparison: std::vector<int>::iterator it; it = std::unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ? // ^ myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10 // using predicate comparison: std::unique (myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content: std::cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}
Output:
myvector contains: 10 20 30 20 10
Complexity
For non-empty ranges, linear in one less than the distance between first and last: Compares each pair of consecutive elements, and possibly performs assignments on some of them.Data races
The objects in the range[first,last)
are accessed and potentially modified.Exceptions
Throws if any of pred, the element comparisons, the element assignments or the operations on iterators throws.Note that invalid arguments cause undefined behavior.
另一个实例:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
/* 删除容器内重复元素,分三步处理
* 1、先排序容器元素
* 2、取得重复元素首部迭代器
* 3、删除重复元素
*/
using namespace std;
int main()
{
int a[]={1,2,3,1,2,4,4,5};
const int len = sizeof(a)/sizeof(int);
vector<int> va(len);// 定义一个与数组等长的容器
copy(a, a + len, va.begin());
ostream_iterator<int, char> oi(cout," ");//定义一个输出流迭代器
copy(va.begin(), va.end(), oi);
cout << " 容器内元素顺序输出结果" << endl;
// sort(两个参数)默认升序排列元素,如果不是基本类型,请重载操作符opearte<.
// sort (三个参数) sort(va.begin(),va.end(),handle_v);
// 自己写函数实现处理两个元素参数 bool handle_v(const int & a,cont int & b){};
sort(va.begin(), va.end());
vector<int>::iterator it = unique(va.begin(),va.end());
va.erase(it, va.end());
copy(va.begin(), va.end(), oi);
cout << " 删除重复元素后顺序输出结果" << endl;
return 0;
}
- vector去重--unique
- 关于STL中vector使用unique()去重的问题
- 关于STL中vector使用unique()去重的问题
- 关于STL中vector使用unique()去重
- unique() 去重函数
- unique() 去重函数
- unique() 去重函数
- unique() 去重函数
- unique 去重函数
- unique() 去重函数
- unique去重原理
- unique去重函数
- vector 去重
- Vector去重
- c++vector去重
- vector 去重模板
- vector去重方法
- L2-005. 集合相似度 unique的应用 去重 vector
- github试水。。。
- Android Service学习之AIDL, Parcelable和远程服务
- 批量kill进程/线程
- 专题二 1003
- ACM第二次练习—1013&1014
- vector去重--unique
- Leetcode - Balanced Binary Tree
- leetcode——86——Partition List
- MJRefresh代码分析
- java命令行路径
- 电子相册系统(三)使用技术
- Install Git--MacOs X
- exynos4412 的FIMD 介绍
- 欢迎使用CSDN-markdown编辑器