|
传送门:http://cplusplus.com/reference/algorithm/unique/
unique是把不同的元素覆盖到数组前面,但是vector的size不变,如果要改变的话(如果用二分。。如果size不变的话会出错),用v.resize.改变size。。。
二分查找#include <iostream>
#include <algorithm>#include <cstdio>
#include <vector>
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
int main () {
int myints[] = {9,2,3,4,5,4,3,2,1};
vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1
// using default comparison:
sort (v.begin(), v.end());
cout << "looking for a 3... ";
if (binary_search (v.begin(), v.end(), 3))
cout << "found!\n"; else cout << "not found.\n";
// using myfunction as comp:
sort (v.begin(), v.end(), myfunction);
cout << "looking for a 6... ";
if (binary_search (v.begin(), v.end(), 1, myfunction))
cout << "found!\n"; else cout << "not found.\n";
for(int i=0;i<9;i++)
printf("%d ",myints[i]);
return 0;
}