c++ - 使用 struct 作为 map 的 KEY 和 VALUE。 find() 操作给出错误

标签 c++ dictionary struct operator-overloading

C++ 代码:

#include <iostream>
#include <map>
#include <string>

using namespace std;

struct keyInfo
{
  string Key1; 
  string Key2; 

  bool keyInfo::operator <(keyInfo &A) const
    { return ((this->Key1<A.Key1)&&(this->Key2<A.Key2)); }
};

struct valueInfo
{ 
  int value1; 
  int value2; 
  int value3; 

  valueInfo(const int A,const int B,const int C) : 
    value1(A),value2(B),value3(C) {}
};
typedef std::map<keyInfo, valueInfo> MapTYPE;

int main()
{
  MapTYPE TMap;
  keyInfo K;
  K.Key1="main";
  K.Key2="i";
  valueInfo V(-2,-3322,9000);

  TMap.insert(MapTYPE::value_type(K,V));
  MapTYPE::iterator It1=TMap.find(K);
  It1=TMap.find(K);
  if(It1!=TMap.end())
    std::cout<<"Success(K): "<<It1->second.value2<<std::endl;

  keyInfo E;
  E.Key1="main";
  E.Key2="j";
  //TMap.insert(std::pair<keyInfo,valueInfo>(E,V));
  MapTYPE::iterator It2=TMap.find(E);
  if (It2!=TMap.end())
     std::cout<<"Success(E): "<<(It2->second).value3<<std::endl;

  cin.get();
  return 0;
 }

当我编译这段代码时,它给了我错误:

error C2679: binary '<' : no operator found which takes a right-hand operand of type 'const keyInfo1' (or there is no acceptable conversion)

请让我知道我要去哪里错了? 非常感谢任何帮助。 谢谢。

我尝试实现自己的运算符并在 map<> 中使用,代码如下:

#include <iostream>
#include <map>
#include <string>

using namespace std;

struct keyInfo
{
  string Key1; 
  string Key2; 

  /*bool keyInfo::operator()(keyInfo const& Left,keyInfo const& Right) const{
      return ((Left.Key1<Right.Key1)&&(Left.Key2<Right.Key2));
  }*/
};

struct LessComparer{
    bool operator()(keyInfo const& Left,keyInfo const& Right) const{
        return !(Left.Key1==Right.Key1 && Left.Key2==Right.Key2);
    }
};

struct valueInfo
{ 
  int value1; 
  int value2; 
  int value3; 

  valueInfo(const int A,const int B,const int C) : 
    value1(A),value2(B),value3(C) {}
};
typedef std::map<keyInfo, valueInfo, LessComparer> MapTYPE;

int main()
{
  MapTYPE TMap;
  keyInfo K;
  K.Key1="main";
  K.Key2="i";
  valueInfo V(-2,-3322,9000);

  TMap.insert(MapTYPE::value_type(K,V));
  MapTYPE::iterator It1=TMap.find(K);
  It1=TMap.find(K);
  if(It1!=TMap.end())
    std::cout<<"Success(K): "<<It1->second.value2<<std::endl;

  keyInfo E;
  E.Key1="main";
  E.Key2="j";
  //TMap.insert(std::pair<keyInfo,valueInfo>(E,V));
  MapTYPE::iterator It2=TMap.find(E);
  if (It2!=TMap.end())
     std::cout<<"Success(E): "<<(It2->second).value3<<std::endl;

  cin.get();
  return 0;
 }

这里我使用 operator() 返回 0 当且仅当 Left 和 Right 的 Key1 和 Key2 都相等。我认为这与 map::less 的工作方式相同,我的意思是它仅在满足相等条件时才返回 false。

它在第一种情况下工作正常,即 TMap.find(K) 找到相同的键。但是在第二种情况下调用期间,即 TMap.find(E) 它会弹出一条错误消息:

"Debug assertion failed"
Expression: Invalid operator <

最佳答案

您的声明operator<关闭。

struct keyInfo
{
  string Key1; 
  string Key2; 

  bool keyInfo::operator <(keyInfo &A) const
    { return ((this->Key1<A.Key1)&&(this->Key2<A.Key2)); }
};

... 有几个原因:

  1. 在类中以类名作为声明前缀是错误的。只有在类外定义它时才应该这样做。一些编译器是宽松的,但标准说你不应该这样做。
  2. 无法编译的原因是 operator<应该接受它的两个操作数作为值(对于简单的事情)或 const& .在这里你忘了 const对于 A .
  3. 定义不正确,你的operator<将其语义作为 antisymmetry 的属性关闭不被尊重。
  4. 建议将二元运算符声明为类外的自由函数。

总而言之,正确的声明和定义是:

struct keyInfo {
  std::string Key1;
  std::string Key2;
};

inline bool operator<(keyInfo const& left, keyInfo const& right) {
  if (left.Key1 < right.Key1) { return true; }
  if (left.Key1 > right.Key1) { return false; }
  return left.Key2 < right.Key2;
}

如果您可以使用 Boost,实现它的“简单”方法是:

inline bool operator<(keyInfo const& left, keyInfo const& right) {
  return boost::tie(boost::cref(left.Key1) , boost::cref(left.Key2))
       < boost::tie(boost::cref(right.Key1), boost::cref(right.Key2));
}

关于c++ - 使用 struct 作为 map 的 KEY 和 VALUE。 find() 操作给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9647110/

相关文章:

java - 在映射中查找匹配的键和值

Python - 将文本文件读入字典

c++ - Visual C++ 中的位域问题

c++ - 在 vector 中按一个词查找行

c++ - 如何在 C++ 中实现一个函数作为一些可选参数?

c++ - 扩展此 C++ 工厂实现的最佳方法?

javascript - 将Leaflet标记添加到Leaflet中的geojson数据中

c++ - 是否符合 libuuid rfc4122 标准

c - 家庭作业的段错误。不知道为什么

c - 使用 malloc 初始化结构内部的结构?