如何对Mac密钥的集合进行排序
How to sort collection of mac key
我需要存储设备列表。每个设备都有唯一的 MAC 地址。我想使用 stl 映射存储它,mac 地址作为密钥。
我定义了结构:
struct MacBytes
{
char byte1;
char byte2;
char byte3;
char byte4;
char byte5;
char byte6;
bool operator <(const MacBytes& rhs) const
{
//add implamention here
}
}
任何实现"运算符<"功能的建议(对于 stl map 是必需的(,而无需使用许多 if 语句。或者可以向另一个陈述提出建议。
不要使用具有一堆单独的char
字段的结构,而是使用 std::array<char, 6>
。 这是一种更简单的表示数据的方法,std::array
已经有了operator<
,因此您无需编写自己的数据。
使用std::tie
(或者可能考虑将MacBytes
实现为直接包含std::tuple
(:
operator <(const MacBytes& lhs, const MacBytes& rhs)
{
return std::tie(lhs.byte1,lhs.byte2,lhs.byte3,lhs.byte4,lhs.byte5,lhs.byte6) <
std::tie(rhs.byte1,rhs.byte2,rhs.byte3,rhs.byte4,rhs.byte5,rhs.byte6);
}
相关文章:
- 二叉排序树无法编译
- 从具有自定义排序的向量创建集合
- 有没有办法在 c++ 中按更大的 int 和更小的对对 int 和对的集合进行排序?
- C++ SFINAE:如果可能,对 STD 集合进行排序
- 有没有办法为集合指定不同的唯一性和排序标准
- 如何对Mac密钥的集合进行排序
- 模板集合的不同排序方法
- 维护多集中数字的排序列表和另一个集合中的累积总和
- 用于访问集合c++的排序子集的数据结构
- 如何制作一个集合,其中元素使用局部变量进行排序
- C++ 集合有序迭代 - 是迭代集合的结果,按标准排序
- 对基于地图C++集合重新排序的有效方法
- 使用Functor在集合中生成自定义排序方法
- 使用集合对向量进行排序
- 哪一种方法是更快的向量(插入后排序)或集合
- 无序集合哈希表中元素的排序
- c++标准库是否有按插入顺序排序的集合
- 如何在c++中获得一个自适应排序的整数集合
- 影响指针集合排序的因素(具体示例)
- 将已排序的集合与映射合并