[英]error: no match for ‘operator<’ in ‘__x < __y’ when trying to insert in two map
In code there are two map.One store pair and other store where Values is class with 5 variable with data type string,int,string,int,int.but during inserting in the second map i am getting error g++ error: no match for 'operator<' in '__x < __y' when trying to insert in map.在代码中有两个映射。一个存储对和另一个存储,其中 Values 是具有 5 个变量的类,数据类型为 string、int、string、int、int。但是在插入第二个映射期间,我收到错误 g++ 错误:不匹配尝试插入地图时,'__x < __y' 中的 'operator<'。 (Note Keys and Values in first map changes to Values,Key in second map)
(注意第一个映射中的键和值更改为值,第二个映射中的键)
How to solve it.如何解决。
class Values
{
private:
std::string C_addr;
int C_port;
std::string S_addr;
int S_port;
int C_ID;
public:
Values(std::string,int,std::string,int,int);
void printValues();
};
Values :: Values(std::string Caddr,int Cport,std::string Saddr,int Sport,int Cid)
{
C_addr=Caddr;
C_port=Cport;
S_addr=Swaddr;
S_port=Sport;
C_ID=Cid;
}
void Values::printValues()
{
cout << C_addr<<":" <<C_port<<":" << S_addr <<":" <<S_port << ":"<<C_ID <<endl;
}
map<int, Values> items;
map<Values,int> itemscopy;
Values connection (inet_ntoa(Caddr.sin_addr),ntohs(Caddr.sin_port),inet_ntoa(Saddr.sin_addr),ntohs(Saddr.sin_port),CID);
for(unsigned int key=0;key<=30000; )
{
map<int,Values>::const_iterator itemsIterator=items.find(key);
if(itemsIterator==items.end())
{
items.insert(pair<int, Values> (key, connection));
{
map<Values,int>::const_iterator itemsIterator1;
if(itemsIterator1==itemscopy.end())
itemscopy.insert(pair<Values,int> (connection, key));
}
break;
}
else
{
cout<<"already exist";
key=key+1;
}
}
The compiler does not know in which order to insert keys in the map.编译器不知道以什么顺序在映射中插入键。 You have to define some order relation for class Values.
您必须为类值定义一些顺序关系。
You need to define operator < for your class.您需要为您的类定义运算符 <。 For example you can do it the following way or something else
例如,您可以按照以下方式或其他方式进行
class Values
{
private:
std::string C_addr;
int C_port;
std::string S_addr;
int S_port;
int C_ID;
public:
Values(std::string,int,std::string,int,int);
void printValues();
bool operator <( const Values &rhs ) const
{
return ( C_ID < rhs.C_ID );
}
};
For your second map the key type is not compareable.对于您的第二张地图,密钥类型不可比较。
map<Values,int>
is essentially this map<Values,int>
本质上是这样的map<Values, int, std::less<Values>, std::allocator<std::pair<const Values, int>
. map<Values, int, std::less<Values>, std::allocator<std::pair<const Values, int>
。 Sinec you don't have an bool operator<
for your Value type less will not compile.如果您的 Value 类型没有
bool operator<
,那么 Sinec 将无法编译。
So you can either define an bool operator<
for your class or you create the map with an own comparison function.因此,您可以为您的类定义
bool operator<
,也可以使用自己的比较函数创建映射。
Implement bool operator<(const Values& other) const
member function in the Values
class that will enable map<Values, int>
to sort the keys of type Values
.在
Values
类中实现bool operator<(const Values& other) const
成员函数,这将使map<Values, int>
能够对Values
类型的键进行排序。 The map stores key-value pairs and the keys are sorted, so you need to provide the comparison operator for them.映射存储键值对并且键已排序,因此您需要为它们提供比较运算符。 When you instantiate
map<Values, int>
you are saying that you will use Values
as keys and ints
as values for that map.当您实例化
map<Values, int>
您是说您将使用Values
作为该映射的键和ints
作为值。
Here is a small working example, where the C_ID is taken as the comparison argument for Values
:这是一个小的工作示例,其中 C_ID 被用作
Values
的比较参数:
#include <map>
class Values
{
private:
std::string C_addr;
int C_port;
std::string S_addr;
int S_port;
int C_ID;
public:
Values(std::string first,int second,std::string third,int fourth,int fifth)
:
C_addr(first),
C_port(second),
S_addr(third),
S_port(fourth),
C_ID(fifth)
{};
bool operator<(const Values& other) const
{
return C_ID < other.C_ID;
}
};
using namespace std;
int main(int argc, const char *argv[])
{
map<Values, int> mymap;
mymap.insert(std::make_pair(Values("test", 0, "me", 1, 2), 0));
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.