STL中的unique
去重函数。
unique一般现需要对数组排序后再使用,返回值是不重复数组的哨兵位。
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main(){ int a[] = {1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 9, 10}; vector<int> vec(a, a + 16); sort(vec.begin(), vec.end()); vector<int>::iterator pos, it; pos = unique(vec.begin(), vec.end()); for(it = vec.begin(); it != pos; ++it) cout << *it << ' '; cout << endl; getchar(); return 0; }
输出:
1 2 3 4 5 6 7 8 9 10