C++ 在STL映射中查找字符串键的上界
在STL映射中查找字符串键的上界 我试图在STL映射中找到字符串键的上界,但没有给出确切的结果。如果你能运行这个程序,你会发现结果很奇怪,上界和下界都指向“qwerzzx” 我的代码中是否有任何错误,或者我误解了上限运算C++ 在STL映射中查找字符串键的上界,c++,map,C++,Map,在STL映射中查找字符串键的上界 我试图在STL映射中找到字符串键的上界,但没有给出确切的结果。如果你能运行这个程序,你会发现结果很奇怪,上界和下界都指向“qwerzzx” 我的代码中是否有任何错误,或者我误解了上限运算 #include<iostream> #include<cstring> #include <map> using namespace std; int main() { map<string, int> testmap
#include<iostream>
#include<cstring>
#include <map>
using namespace std;
int main()
{
map<string, int> testmap;
map<string, int>::iterator poslow;
map<string, int>::iterator posup;
testmap.insert(make_pair<string, int>("asdfghjkliopp", 1));
testmap.insert(make_pair<string, int>("asdfghjklioppswert", 1));
testmap.insert(make_pair<string, int>("sdertppswert", 1));
testmap.insert(make_pair<string, int>("sdertppswedertyuqrt", 1));
testmap.insert(make_pair<string, int>("qwerzzx", 1));
testmap.insert(make_pair<string, int>("qwerzzxasdf", 1));
testmap.insert(make_pair<string, int>("qwsdfgqwerzzx", 1));
testmap.insert(make_pair<string, int>("xcvbqwsdfgqwerzzx", 1));
testmap.insert(make_pair<string, int>("xcvbqwsdersdfgqwerzzx", 1));
poslow = testmap.lower_bound("qw");
posup = testmap.upper_bound("qw");
cout<<"Lower POS ::: "<<poslow->first<<" UPPER POS :: "<<posup->first<<"\n";
testmap.erase(poslow, posup);
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
地图测试地图;
迭代器poslow;
迭代器posup;
insert(make_pair(“asdfghjkliopp”,1));
testmap.insert(make_pair(“asdfghjklioppswert”,1));
testmap.insert(make_pair(“sdertppswert”,1));
testmap.insert(make_pair(“sdertppswedertyuqrt”,1));
testmap.insert(制作成对(“qwerzzx”,1));
testmap.insert(生成一对(“qwerzzxasdf”,1));
testmap.insert(生成一对(“qwsdfgqwerzzx”,1));
testmap.insert(创建_对(“xcvbqwsdfgqwerzzx”,1));
testmap.insert(成对(“xcvbqwsdersdfgqwerzzx”,1));
poslow=testmap.lower_界(“qw”);
posup=testmap.上界(“qw”);
cout上界给出了插入参数的最后一个位置,同时仍然保持序列的排序(而下界给出了第一个这样的位置)。由于“qw”在词典上比“qwerzzx”小,所以这是该单词的下界和上界
换句话说,[lower\u bound,upper\u bound)
是等于参数的元素间隔,在本例中,它是空的
如果您打算查找带有此前缀的最后一个单词,可以尝试在末尾添加一些字符,以确保其在字典上比地图中的最后一个字符大。例如,如果您只有字母字符,则可以在ASCII表中查找'z'
之后的字符,并将其附加到“qw”中。通过这种方式,您应该能够获得一个迭代器,在您的例子中,“xcvbqwsdfgqwerzzx”。上限返回大于搜索键的项。下限返回大于或等于的项。在这种情况下,它们都是相同的,因为映射中没有相等的项
其目的是,它们都返回一个位置,在该位置上项目可以在之前插入,并且仍然保留排序顺序。下限将其放在范围的前面,上限将其放在末尾。一个具有可编译源代码的问题。做得好,先生!@john这是一个相当低的条。太糟糕了非常罕见,值得评论。