C++stl's set and Multiset containers have their own lower_bound () function and the Upper_bound () function, which can also be used in other containers, such as array, vector, and so on.
It is consistent in thought when used, but slightly different in usage. I use vectors and multiset to illustrate these two containers.
First, Vector
The Lower_bound () function returns the first position that is not less than the key of a given element
The Upper_bound () function returns the position of the first key greater than the given element
Usage:
#include <bits/stdc++.h>using namespacestd;intmain () {vector<int>v; V.push_back (1); V.push_back (2); V.push_back (2); V.push_back (3); V.push_back (3); V.push_back ( +); V.push_back (0); V.push_back ( About); V.push_back ( -); Vector<int>:: Iterator it; for(it = V.begin (); It! = V.end (); it++) {cout<<*it<<" "; } cout<<"\ n----------\ n"; Sort (V.begin (), V.end ()); for(it = V.begin (); It! = V.end (); it++) {cout<<*it<<" "; } cout<<"\ n----------\ n"; //the use of Lower_bound () and Upper_bound () must be in an orderly situation:
Note that the subscript is starting from 0.vector<int>:: Iterator lower,upper;
Lower= Lower_bound (V.begin (), V.end (),2); Upper= Upper_bound (V.begin (), V.end (),3); cout<<"Subscript for the position of the first element not less than 2"<< (Lower-v.begin ()) <<"\ n"; cout<<"The first subscript that is greater than the position of element 3"<< (Upper-v.begin ()) <<"\ n" ; return 0;}
Second,Multiset
The Lower_bound () function returns the first value that is not less than the key of a given element
The Upper_bound () function returns the first value that is greater than the given element key
Usage:
#include <iostream>#include<Set>using namespacestd;intMain () {multiset<int>Mymultiset; Multiset<int>:: Iterator Itlow,itup; //usage: The same must be done in an orderly manner for(intI=1; i<8; i++) Mymultiset.insert (i*Ten);//Ten .Itlow = Mymultiset.lower_bound ( -);//^ The value at the location you are seekingItup = Mymultiset.upper_bound ( +);//^ The value at the location you are seekingmymultiset.erase (Itlow,itup); //Delete the interval element from 30 to 50, remaining tencout<<*itlow<<Endl; cout<<*itup<<Endl; cout<<"Mymultiset contains:"; for(multiset<int>::iterator It=mymultiset.begin (); It!=mymultiset.end (); ++it) cout<<' '<< *it; cout<<"\ n"; return 0;}
reference resources;
Http://www.cplusplus.com
Understanding of Lower_bound () Upper_bound () in C + + STL