【发布时间】:2012-01-17 13:23:03
【问题描述】:
我来自一个相当实用的编程背景,我不习惯(高效)C++ 数据结构。我需要一个数据结构来保存多个元素,如struct element 中所述。在集合中,字段 id 应该是唯一的。
我想像在集合论中那样执行非常快速的集合比较,例如,当比较集合{x1,x2,x3} 和{x4,x5} 我想确定交集{x5}(或{x2},它们在此相等case) 并从其他集合中减去集合,例如{x1,x2,x3} \ {x5} = {x1,x3}.
在 C++ 世界中是否存在……“集合论”数据结构?
struct element {
int id;
float value;
};
struct element x1 = {1, 1.0};
struct element x2 = {2, 2.0};
struct element x3 = {3, 3.0};
struct element x4 = {3, 3.1};
struct element x5 = {2, 2.0};
【问题讨论】:
-
std::set 是您正在寻找的。 cplusplus.com/reference/stl/set 请注意,您需要一个比较运算符。
-
std::setin<set>。 stdcxx.apache.org/doc/stdlibug/8-2.html -
而且,对于 std::set,
中有一些东西,例如 std::set_union cplusplus.com/reference/algorithm
标签: c++ data-structures set-theory