std::map是我们最常用的容器之一,然而 lower_bound, upper_bound很少使用,这里介绍一下经典用法:


k<=10  得到 500
10<k<=20 得到 600
20<k<=30 得到 700




std::map<int, int> mapPrize;
mapPrize[10] = 500;
mapPrize[20] = 600;
mapPrize[30] = 700;


mapPrize.lower_bound(9)->second 500;
mapPrize.lower_bound(10)->second 500;
mapPrize.lower_bound(11)->second 600;
mapPrize.lower_bound(20)->second 600;


mapPrize.lower_bound(21)->second 700;
mapPrize.lower_bound(30)->second 700;


k<10 得到 500;
10 <= k <20 得到 600;
20<= k < 30 得到 700;


mapPrize.upper_bound(9)->second 500;
mapPrize.upper_bound(10)->second 600;
mapPrize.upper_bound(11)->second 600;
mapPrize.upper_bound(20)->second 700;


mapPrize.upper_bound(21)->second 700;
mapPrize.upper_bound(30)->second 运行错误!!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐