无法添加多个键以映射将结构作为键

Unable to add multiple keys to map having struct as key

本文关键字:结构 映射 添加      更新时间:2023-10-16

我正在尝试将多个键值对添加到以结构作为其键和值的映射中。由于某些原因,只有我添加的第一个键值对被添加到映射中,而找不到第二个键值对。

#include<string>
#include<iostream>
#include<map>
using namespace std;
struct RJNodeAddress
{
string m_ip;
string m_port;
bool operator<(const RJNodeAddress &l)const
{
int l_size=l.m_ip.size();
int r_size=l.m_port.size();                                          
return (l_size < r_size);
}
};
struct RJNodeDetails
{
string m_NodeType;
int        m_appId;
};
class RJWebsocket
{
public:
static map<RJNodeAddress,RJNodeDetails> m_Nodes;
};
map<RJNodeAddress,RJNodeDetails> RJWebsocket::m_Nodes;
int main()
{
RJNodeAddress l_node1,l_node2;
RJNodeDetails l_add1,l_add2;
l_node1.m_ip="172.16.129.68";
l_node1.m_port="8766";
l_node2.m_ip="172.16.128.130";
l_node2.m_port="8768";
l_add1.m_appId=0;
l_add1.m_NodeType="CM";
l_add1.m_appId=1;
l_add1.m_NodeType="CM";
RJWebsocket::m_Nodes.insert({l_node1,l_add1});
RJWebsocket::m_Nodes.insert({l_node2,l_add2});
for(const auto& j:RJWebsocket::m_Nodes)
{
cout<<endl<<"Iterating through RJWebsocket::m_Nodes"<<endl;
cout<<endl<<"port: "<<j.first.m_port<<"  "<<"IP: "<<j.first.m_ip<<endl;
}
}

上面的代码给出了以下输出:

Iterating through RJWebsocket::m_Nodes
port: 8766  IP: 172.16.129.68

您的比较运算符做了一些奇怪的事情,我建议依靠std::tuple提供的运算符,如下所示:

#include <tuple>
bool operator<(const RJNodeAddress &l)const
{
return std::tie(m_ip, m_port) < std::tie(l.m_ip, l.m_port);
}

这样,可以正确执行std::map::insert等价性检查,并且 main 函数中的代码段确实构造了两个条目。然后我的机器上的输出是

Iterating through RJWebsocket::m_Nodes
port: 8768  IP: 172.16.128.130
Iterating through RJWebsocket::m_Nodes
port: 8766  IP: 172.16.129.68

请注意,正如@DanielLangr注释中指出的那样,RJNodeAddress也可以直接是std::pair<std::string, std::string>std::tuple<std::string, std::string>的类型别名,它提供了开箱即用的字典编纂operator <并回退到std::string::operator <。这样可以避免手动指定比较,但有两个缺点:它不提供命名struct及其数据成员的表现力,并且其字典顺序比较可能不是您最初想要的。

您的运算符不遵守比较要求

bool operator<(const RJNodeAddress &l)const
{
int l_size=l.m_ip.size();
int r_size=l.m_port.size();                                          
return (l_size < r_size); // Wrong
}

您可能想要类似以下内容:

bool operator<(const RJNodeAddress &rhs)const
{
auto as_tuple = [](const RJNodeAddress & a){std::tie(a.m_ip, a.m_port); };
return as_tuple(*this) < as_tuple(rhs);
}

你的operator<应该比较两个不同的RJNodeAddress,你似乎只看操作数(l(。尝试这样的事情:

bool operator<(const RJNodeAddress &l)const
{
if (m_ip == l.m_ip)
return m_port < l.m_port;
return m_ip < l.m_ip;
}