C++ lower_bound upper_bound

si_yu_ge發表於2020-09-26
#include<vector>
#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( "Boy");
	a.push_back( "Ross");
	a.push_back( "Joey");
	sort( a.begin(), a.end());
	show( a);
	
	//lower_bound( iterator first, iterator last, key) 有序序列中,可插入的最前位置 
	auto first_pos = lower_bound( a.begin(), a.end(), "Boy");
	//upper_bound(  iterator first, iterator last, key) 有序序列中,可插入的最後位置 
	auto last_pos = upper_bound( a.begin(), a.end(), "Boy");
	
	a.insert( first_pos, "Boy");
	show( a);
}