使用比较运算符将 std::string 作为 std::map 中的键
我试图使用 std::string 作为 std::map 中的键,但是我无法正确找到()。我的代码有点复杂而且很大,所以这是一个小程序,演示了我遇到的问题。如果有人能告诉我为什么这不起作用,我将非常感激。
谢谢。
#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");
}
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.
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
删除你的
比较器
,它就会正常工作。问题是,你没有正确执行它。如果将x
放置在y
之前,则应返回true
。或者将==0
更改为<0
或>0
(这并不重要)。Remove your
comparer
and it will work just fine. The thing is, you didn't implement it properly. It should returntrue
ifx
is to be placed beforey
. Or change==0
to<0
or>0
(it doesn't really matter).comparer::operator()
应该返回运算符 < 的值,而不是运算符 == 的值。comparer::operator()
should return value of operator <, not of operator ==.std::map
(和set
及其multi
变体)强制执行严格弱排序。如果字符串相等,则返回 true。比较器应返回第一个字符串是否应位于第二个字符串之前。返回
x.compare(y) < 0
或者只是将比较函子排除在外。std::map
(andset
and theirmulti
variants) enforce strict weak ordering.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.通常不需要自定义比较操作(您通常可以只使用 std::map),但在使用(不是很短的)字符串作为时进行一些优化键并传递字符串常量(char*常量)(或 std::string_view 或任何其他可以轻松与 std::string 进行比较而无需转换为的类型
std::string
) 到contains()
或find()
,请参阅 https://www.cppstories.com/2021/heterogeneous-access-cpp20/。(此外,您的 comparer::operator() 可以采用 const 字符串引用,以避免也复制到那里:bool operator()(const std::string& x, const std::string& ; y) 常量...)
The custom compare operation is not usually required (you can normally just use
std::map<std::string, int>
), but for a bit of optimization when using (not very short) strings as keys and passing a string constant (char* constant) (orstd::string_view
or any other type that can be easily compared tostd::string
without converting tostd::string
) tocontains()
orfind()
, see https://www.cppstories.com/2021/heterogeneous-access-cpp20/.(Also your
comparer::operator()
could take const string references, to avoid copying there as well:bool operator()(const std::string& x, const std::string& y) const
...)这应该有效:
This should work: