使用比较运算符将 std::string 作为 std::map 中的键

发布于 2024-12-19 05:30:19 字数 995 浏览 1 评论 0原文

我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无尽的现实 2024-12-26 05:30:19

删除你的比较器,它就会正常工作。问题是,你没有正确执行它。如果将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 return true if x is to be placed before y. Or change ==0 to <0 or >0 (it doesn't really matter).

神经暖 2024-12-26 05:30:19

comparer::operator() 应该返回运算符 < 的值,而不是运算符 == 的值。

comparer::operator() should return value of operator <, not of operator ==.

ぺ禁宫浮华殁 2024-12-26 05:30:19

std::map(和set及其multi变体)强制执行严格弱排序

x.compare(y) == 0;

如果字符串相等,则返回 true。比较器应返回第一个字符串是否应位于第二个字符串之前。返回 x.compare(y) < 0 或者只是将比较函子排除在外。

std::map (and set and their multi variants) enforce strict weak ordering.

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.

笔芯 2024-12-26 05:30:19

通常不需要自定义比较操作(您通常可以只使用 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) (or std::string_view or any other type that can be easily compared to std::string without converting to std::string) to contains() or find(), 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...)

一花一树开 2024-12-26 05:30:19

这应该有效:

#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");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文