std:: lower_bound
该函数返回范围内第一个不小于(大于或等于)指定val的值。如果序列中的值都小于val,则返回last.序列应该已经有序!
eg:
#include#include #include using namespace std;int main(int argv,char **argc){ vector v1{ 1,2,3,4}; cout<<"v1="; for(int i:v1) cout< <<" "; cout<
截图:
std:: upper_bound
该函数返回范围内第一个 大于 指定val的值。如果序列中的值都小于val,则返回last.序列应该已经有序!
eg:
#include// std::cout#include // std::lower_bound, std::upper_bound, std::sort#include // std::vectorint main () { int myints[] = { 10,20,30,30,20,10,10,20}; std::vector v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector ::iterator low,up; low=std::lower_bound (v.begin(), v.end(), 20); // ^ up= std::upper_bound (v.begin(), v.end(), 20); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0;}
截图:
另外,在map里的使用方法:
// map::lower_bound/upper_bound#include#include
结果:
a => 20e => 100