无法添加多个键以映射将结构作为键
Unable to add multiple keys to map having struct as key
我正在尝试将多个键值对添加到以结构作为其键和值的映射中。由于某些原因,只有我添加的第一个键值对被添加到映射中,而找不到第二个键值对。
#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;
}
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- C++匿名结构作为std::映射值
- 添加静态constexpr成员是否会更改结构/类的内存映射
- C++:映射结构中的结构指针
- 无法添加多个键以映射将结构作为键
- 如何在 c++ 中创建结构向量的映射
- c++ 嵌套结构映射找不到成员
- 带有结构映射的 C++ 向量?
- Unity C#和C 结构映射
- 使用 std::vector 将C++结构映射到 C#
- C++stl::使用结构映射find
- 如何在C++中把结构映射到字符正确的方法
- 更好的数据结构映射函数与多个参数(键)
- 将PolarSSL x509_cert结构映射到pkcs# 11 X.509对象
- 为什么我的JNA结构映射产生奇怪的字段值
- 在函数中填充结构映射的最好方法是什么?
- 如何解决声明结构映射时的编译错误
- 数据结构-C++映射:需要智能算法
- 使用字符串键初始化结构映射