如何查询网站空间商,网站上怎么做星星评分,ppt模板免费下载素材网站,智能科技网站模板下载最近在工作中遇到了std::map中的lower_bound与upper_bound#xff0c;再次记录下其功能和使用方式。
std::mapchar, int mp;
mp.lower_boundkey #xff1a; 返回的是第一个大于、等于key的iterator#xff0c;如果没有则返回空。
mp.upper_boundkey…最近在工作中遇到了std::map中的lower_bound与upper_bound再次记录下其功能和使用方式。
std::mapchar, int mp;
mp.lower_boundkey 返回的是第一个大于、等于key的iterator如果没有则返回空。
mp.upper_boundkey 返回的是第一个大于key的iterator如果没有则返回空 例子如下
// map::lower_bound/upper_bound
#include iostream
#include mapint main ()
{std::mapchar,int mymap {{a, 20}, {b, 40}, {c, 60}, {d, 80}, {e, 100}};// 否则使用下面的赋值方式
#if 0mymap[a]20;mymap[b]40;mymap[c]60;mymap[d]80;mymap[e]100;
#endifauto itlow mymap.lower_bound(b); // itlow points to bauto itup mymap.upper_bound(d); // itup points to e (not d!)std::cout itlow itlow-first itlow-second \n;std::cout itup itup-first itup-second \n;auto upper mymap.upper_bound(e);std::cout upper upper-first upper-second \n;mymap.erase(itlow,itup); // erases [itlow,itup)std::cout print content \n;for (std::mapchar,int::iterator itmymap.begin(); it!mymap.end(); it)std::cout it-first it-second \n;return 0;
}
结果 总结
1、使用upper_bound(key)时如果没有找到大于key的iterator时返回为空
2、lower_bound(key)返回的是大于、等于key的iterator如果没有返回空。