【问题标题】:std::string as a key in std::map using a compare operator使用比较运算符将 std::string 作为 std::map 中的键
【发布时间】:2012-01-12 13:45:18
【问题描述】:

我正在尝试使用 std::string 作为 std::map 中的键,但是,我无法正确地 find()。我的代码有点复杂而且很大,所以这是一个演示我遇到的问题的小程序。如果有人能告诉我为什么这不起作用,我将非常感激。

谢谢。

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

【问题讨论】:

    标签: c++ key std stdstring stdmap


    【解决方案1】:

    删除您的comparer,它会正常工作。问题是,您没有正确实施它。如果将x 放在之前 y,它应该返回true。或者把==0改成&lt;0或者&gt;0(没关系)。

    【讨论】:

    • 我将比较器更改为返回 x.compare(y)
    • 不客气,但您肯定误会了删除。如果您将所有出现的std::map&lt;std::string, int, comparer&gt; 替换为std::map&lt;std::string, int&gt;,它肯定会起作用。
    • 我在主应用程序中遇到的问题是,在添加新对之前,我必须检查地图中的重复项。但是,它没有正确检测到重复项
    • 我说的是这段代码,当然不是你的应用程序。至于重复,map 不允许与multimap 有任何不同。
    • 实际上并非如此;似乎不是这样,当我添加两个具有相同键值的对时,遍历地图会显示 2 个具有相同键的条目。
    【解决方案2】:

    comparer::operator() 应该返回运算符

    【讨论】:

      【解决方案3】:

      std::map(和set 及其multi 变体)强制执行strict weak ordering

      x.compare(y) == 0;
      

      如果字符串相等,将返回true。比较器应该返回第一个字符串是否应该在第二个字符串之前。要么返回x.compare(y) &lt; 0,要么直接离开你的比较函子。

      【讨论】:

        【解决方案4】:

        这应该可行:

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

        【讨论】:

          猜你喜欢
          • 2011-06-23
          • 1970-01-01
          • 2012-01-06
          • 1970-01-01
          • 2014-04-14
          • 1970-01-01
          • 2014-08-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多