Nik*_*ita 1 c++ dictionary stl
我在stl和范围[lo,hi]有一张地图.
找到适合此范围的地图中所有元素的最佳方法是什么?
UDT:
关于upper_bound和low_bound的问题是:
例如,我有一个集合或地图{1,2,7,8},我的范围是[3,6].lower_bound将指向7,upper_bound将指向2.假设我不想删除smith,而只是写出范围中的所有元素.我该怎么做?在这种情况下擦除会有多大的复杂性?
如果您使用的是std :: map,它已经排序,您可以使用lower_bound/upper_bound来自cplusplus.com的示例:
// map::lower_bound/upper_bound
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator itlow,itup;
mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;
itlow=mymap.lower_bound ('b'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!)
mymap.erase(itlow,itup); // erases [itlow,itup)
// print content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5331 次 |
最近记录: |