#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
void show( const vector <string> a){
for(auto p = a.begin(); p != a.end(); p ++)
cout << *p << endl;
cout << endl;
}
int main(){
vector <string> a;
a.push_back( "ACM");
a.push_back( "Joey");
a.push_back( "Ross");
a.push_back( "ACM");
a.push_back( "Apple");
show( a);
sort( a.begin(), a.end());
show( a);
//binary_search( iterator first, iterator last, key) 只用于有序容器 return bool
bool p = binary_search( a.begin(), a.end(), "Apple");
if( p) cout << "found" << endl;
else cout << "not found" << endl;
cout << endl;
//***排序后 可用unique来删除相邻的重复的值
//unique( iterator first, iterator last) 返回一个指向容器末尾的迭代器(类似remove)
auto i = unique( a.begin(), a.end());
//***调整容器大小
a.erase( i, a.end());
show( a);
}
C++ binary_search unique
最新推荐文章于 2024-05-12 22:02:06 发布