使用struct作为KEY和VALUE for map.find()操作给出错误

N D*_*are 1 c++ struct operator-overloading map

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;
 }
Run Code Online (Sandbox Code Playgroud)

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

错误C2679:二进制'<':没有找到哪个运算符采用'const keyInfo1'类型的右手操作数(或者没有可接受的转换)

请让我知道我哪里出错了?任何帮助都非常感谢.谢谢.

我试图实现自己的运算符并在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;
 }
Run Code Online (Sandbox Code Playgroud)

这里我使用operator()返回0,如果左和右的Key1和Key2都相等.我认为这与map :: less的工作方式相同,我的意思是只有满足相等条件才返回false.

它在第一种情况下工作正常,即TMap.find(K),其中找到相同的密钥.但在调用第二种情况,即TMap.find(E)时,会弹出一个错误说:

"Debug assertion failed"
Expression: Invalid operator <
Run Code Online (Sandbox Code Playgroud)

Mat*_* M. 6

你的声明operator<是关闭的.

struct keyInfo
{
  string Key1; 
  string Key2; 

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

......有几个原因:

  1. 在声明前面添加类名中的类名是错误的.如果你在课外定义它,你应该这样做.有些编译器是松散的,但标准说你不应该这样做.
  2. 你无法编译的原因是它operator<应该接受它的操作数作为值(对于简单的事物)或者const&.在这里,你忘了constA.
  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;
}
Run Code Online (Sandbox Code Playgroud)

如果你可以使用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));
}
Run Code Online (Sandbox Code Playgroud)