• C++ 中集合以及集合的相关操作


    在数学中,集合是最基本的概念之一。编程时,我们不可避免地会涉及到集合及其相关操作。在 C++ 中,标准模板库(STL)提供了 std::set/std::unordered_set 两种传统意义上的集合(除此之外,还有 std::multiset 和 std::unordered_multiset)。

    其中,std::set(和 std::multiset)定义在头文件 set 当中,从 C++98 起就有支持;而 std::unordered_set(和 std::unordered_multiset)则定义在头文件 unordered_set 当中,从 C++11 开始支持。

    STL库中有丰富的集合运算方法,我们可以使用它们快速完成交集、并集、差集、对称差集的运算。集合运算的前提是两个集合必须按照同样的规则排序就绪,否则不能进行集合运算!
    - map,set是有序集合,可以直接参加运算;vector是无序集合,参与运算前必须首先排序.

    此处的集合可以为std::set,也可以是std::multiset,但是不可以是hash_set以及hash_multiset。为什么呢?因为set_intersection要求两个区间必须是有序的(从小到大排列),std::set和std::multiset为有序序列,而hash_set以及hash_multiset为无序序列。   

    交集(set_intersection) A∩BA∩B
    OutputIt set_intersection( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2, OutputIt d_first );
    并集(set_union) A∪BA∪B
    OutputIt set_union( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first );
    差集(set_difference) A−BA−B
    OutputIt set_difference( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first );
    对称差集(set_symmetric_difference) A∪B−(A∩B)A∪B−(A∩B)
    OutputIt set_symmetric_difference( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt d_first );
     

    交集(intersection)

            交集是集合运算中经常会用到的计算,其表达是两个集合共有的部分(图中红色区域)

            STL中有set_intersection方法可以实现该功能。它是C++17开始支持的方法,声明于中。其中一种形式是

    1. template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 >
    2. ForwardIt3 set_intersection( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
    3. ForwardIt2 first2, ForwardIt2 last2,
    4. ForwardIt3 d_first );

    第一二个参数是某个集合的起止迭代器,第二三个参数是另一个集合的起止迭代器。这两个待比较集合要求是有序的。最终得到的交集保存在第五个参数所指向的集合的起始迭代器位置。

    1. #include
    2. #include
    3. #include
    4. #include
    5. int main() {
    6. std::vector<int> a{1, 3, 3, 4, 4, 5, 6};
    7. std::vector<int> b{2, 3, 4, 4, 5, 5, 7};
    8. std::sort(a.begin(), a.end()); //保证集合是有序的
    9. std::sort(b.begin(), b.end()); //保证集合是有序的
    10. std::vector<int> result;
    11. std::set_intersection(a.begin(), a.end(),
    12. b.begin(), b.end(),
    13. std::back_inserter(result));
    14. std::copy(result.begin(), result.end(),
    15. std::ostream_iterator<int>(std::cout, " "));
    16. return 0;
    17. }

    输出结果:

    3 4 4 5 

    note:

    这些集合的操作都是从c++17标准开始执行的,如果编译器低于这个版本怎么办呢?
    可以采用第三方库进行集合操作

    ref:

    Chapter 1. Geometry - 1.80.0

    intersection - 1.80.0

    difference - 1.80.0

    ref:

    谈谈 C++ 中集合的交集和并集 | 始终

    std::set_intersection - cppreference.com

    C++拾取——stl标准库中集合交集、并集、差集、对称差方法_breaksoftware的博客-CSDN博客

    set_intersection

    C++ STL 集合运算_西面来风的博客-CSDN博客_c++ 集合运算

  • 相关阅读:
    RHCE每日一讲
    IEDA 突然找不到了compact middle packages
    Golang vs Rust 为后端选择哪种语言?
    @Value的用法
    软件测试工程师2022年的三阶段总结
    老年人Stroop任务期间颈动脉粥样硬化与脑激活模式的联系:fNIRS研究
    linux内核进程间通信IPC----消息队列
    【算法与数据结构】108、LeetCode将有序数组转换为二叉搜索树
    双向链表的操作
    Spring Security配置个过滤器也这么卷
  • 原文地址:https://blog.csdn.net/wwwlyj123321/article/details/126625083