错误:试图插入两个映射时,“__x

时间:2021-06-13 15:43:49

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. (Note Keys and Values in first map changes to Values,Key in second map)

代码中有两个映射。一个存储对和另一个存储,其中值是具有数据类型字符串、int、string、int、int 5个变量的类。但是在插入第二个映射时,我得到了错误g+ error:在试图插入映射时,“__x < __y”中的“操作符<”没有匹配。(注意第一个映射中的键和值更改为值,第二个映射中的键)

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;
    }
}

3 个解决方案

#1


5  

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

#2


3  

For your second map the key type is not compareable. map<Values,int> is essentially this
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.

对于第二个映射,键类型不可比较。map <值,int> 本质上就是这个map <值,int, std::小于<值> ,std::分配器 。Sinec您没有bool操作符 <值类型为less的不会编译。< p>

So you can either define an bool operator< for your class or you create the map with an own comparison function.

因此,您可以为类定义bool操作符<,或者使用自己的比较函数创建映射。

#3


0  

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. 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.

在Values类中实现bool操作符<(const values&other) const成员函数,该函数将启用map 对类型值的键进行排序。映射存储键-值对,并对键进行排序,因此需要为它们提供比较操作符。当实例化map <值时,int> 表示将使用值作为键,ints作为映射的值。 ,>

Here is a small working example, where the C_ID is taken as the comparison argument for Values:

这里有一个小的工作示例,其中C_ID作为值的比较参数:

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

#1


5  

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

#2


3  

For your second map the key type is not compareable. map<Values,int> is essentially this
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.

对于第二个映射,键类型不可比较。map <值,int> 本质上就是这个map <值,int, std::小于<值> ,std::分配器 。Sinec您没有bool操作符 <值类型为less的不会编译。< p>

So you can either define an bool operator< for your class or you create the map with an own comparison function.

因此,您可以为类定义bool操作符<,或者使用自己的比较函数创建映射。

#3


0  

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. 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.

在Values类中实现bool操作符<(const values&other) const成员函数,该函数将启用map 对类型值的键进行排序。映射存储键-值对,并对键进行排序,因此需要为它们提供比较操作符。当实例化map <值时,int> 表示将使用值作为键,ints作为映射的值。 ,>

Here is a small working example, where the C_ID is taken as the comparison argument for Values:

这里有一个小的工作示例,其中C_ID作为值的比较参数:

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