标称数据C++ std::set_operations(set_difference、set_intersection等)

C++ std::set_operations (set_difference, set_intersection etc.) on nominal data

提问人:Burtan 提问时间:12/29/2019 更新时间:12/29/2019 访问量:67

问:

集被归类为无序的唯一数据容器。因此,集合运算应该适用于标称数据(例如颜色)。但不知何故,像 set_difference 和 set_intersection 这样的 std::set_operations 与自定义比较函数仅适用于序数数据(有序数据,如 int)。有什么想法吗?

auto comp = [](const int a, const int b) -> bool {
    return a == b; // not working
    return a != b; // not working
    return a < b;  // works as expected
}
C++ 设置 std 交集 差异

评论

6赞 KamilCuk 12/29/2019
因为如果第一个参数小于第二个参数,则比较函数应返回 true,而不是如果它们相等或不相等?我不明白。你是在问为什么像这样的算法需要处理有序数据类型吗?好吧,因为返回一个排序列表(我的意思是,排序被保留),你希望它如何与无序类型一起使用?set_intersectionset_intersection
6赞 Evg 12/29/2019
std::set 订购的。
0赞 molbdnilo 12/29/2019
此外,相等性是比严格的弱排序更强的要求,并且使用无序输入实现这些操作的效率要低得多。

答: 暂无答案