• 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++ 集合运算

  • 相关阅读:
    春雷视频添加投屏功能解惑
    易周金融分析 | 银行理财市场渐趋理性;“睡眠信用卡”持续清退
    计算机毕业设计(附源码)python综合众筹网站
    带你着手「Servlet」
    搭建springMvc框架
    探索人工智能的边界:GPT 4.0与文心一言 4.0免费使用体验全揭秘!
    国家开放大学统一训练题
    JVM-类加载机制
    推进农业水价综合改革的意见解读
    虚幻引擎:UEC++如何对JSON文件进行读写?
  • 原文地址:https://blog.csdn.net/wwwlyj123321/article/details/126625083