unique是把不同的元素覆盖到数组前面,但是vector的size不变,如果要改变的话(如果用二分。。如果size不变的话会出错),用v.resize.改变size。。。
#include <iostream> #include <algorithm> #include <vector> using namespace std;
bool 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 vector<int> myvector (myints,myints+9); vector<int>::iterator it;
// using default comparison: it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ? // ^
myvector.resize( it - myvector.begin() ); // 10 20 30 20 10
// using predicate comparison: unique (myvector.begin(), myvector.end(), myfunction); // (no changes)
// print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it;
cout << endl;
return 0; } |
传送门:http://cplusplus.com/reference/algorithm/unique/