C++ 使用比较运算符将 std::string 作为 std::map 中的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8374806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
std::string as a key in std::map using a compare operator
提问by Chromex
I'm trying to use a std::string as a key in a std::map however, i'm unable to find() correctly. My code is somewhat complicated and large so this is a small program that demonstrates the problem I'm having. If someone could tell me why this doesn't work, i'd be very grateful.
我正在尝试使用 std::string 作为 std::map 中的键,但是,我无法正确 find() 。我的代码有点复杂和大,所以这是一个演示我遇到的问题的小程序。如果有人能告诉我为什么这不起作用,我将不胜感激。
Thanks.
谢谢。
#include <stdio.h>
#include <string>
#include <map>
struct comparer
{
public:
bool operator()(const std::string x, const std::string y)
{
return x.compare(y)==0;
}
};
int main(int argc, char *argv[])
{
std::map<std::string, int, comparer> numbers;
numbers.insert(std::pair<std::string,int>("One",1));
numbers.insert(std::pair<std::string,int>("Two",2));
numbers.insert(std::pair<std::string,int>("Three",3));
numbers.insert(std::pair<std::string,int>("Four",4));
numbers.insert(std::pair<std::string,int>("Five",5));
std::map<std::string, int, comparer>::iterator it=numbers.find("Three");
if(it!=numbers.end())
printf("The number is %d\n",(*it).second);
else
printf("Error, the number is not found\n");
}
回答by Michael Krelin - hacker
Remove your comparer
and it will work just fine. The thing is, you didn't implement it properly. It should return true
if x
is to be placed beforey
. Or change ==0
to <0
or >0
(it doesn't really matter).
删除你的comparer
,它会工作得很好。问题是,你没有正确实施它。true
如果x
要放在之前,它应该返回y
。或更改==0
为<0
或>0
(这并不重要)。
回答by Griwes
comparer::operator()
should return value of operator <, not of operator ==.
comparer::operator()
应该返回运算符 < 的值,而不是运算符 == 的值。
回答by Xeo
std::map
(and set
and their multi
variants) enforce strict weak ordering.
std::map
(set
及其multi
变体)强制执行严格的弱排序。
x.compare(y) == 0;
Will return true if the strings are equal. The comparer should return whether the first string should go before the second string. Either return x.compare(y) < 0
or just leave your comparision functor out.
如果字符串相等,则返回 true 。比较器应该返回第一个字符串是否应该在第二个字符串之前。要么返回,x.compare(y) < 0
要么只留下你的比较函子。
回答by u9874315
This should work:
这应该有效:
#include <stdio.h>
#include <string>
#include <map>
struct comparer
{
public:
bool operator()(const std::string x, const std::string y) const
{
return x.compare(y)==0;
}
};
int main(int argc, char *argv[])
{
std::map<std::string, int, comparer> numbers;
numbers.insert(std::pair<std::string,int>("One",1));
numbers.insert(std::pair<std::string,int>("Two",2));
numbers.insert(std::pair<std::string,int>("Three",3));
numbers.insert(std::pair<std::string,int>("Four",4));
numbers.insert(std::pair<std::string,int>("Five",5));
std::map<std::string, int, comparer>::iterator it=numbers.find("Three");
if(it!=numbers.end())
printf("The number is %d\n",(*it).second);
else
printf("Error, the number is not found\n");
}