zoukankan      html  css  js  c++  java
  • STL set_difference set_intersection set_union 操作

    以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的。

    set_difference 这个是求得在第一个容器中有,第二个容器中没有的。set_intersection 求两个容器的交, set_union 求两个容器的并。

    set_symmetric_difference 求两个容器的差。

    最后使用的时候注意要提前分配好最后的盛放容器,其大小最好是两个操作容器的和,然后需要根据返回的迭代器resize一下,看下面的例子。

       1:  // set_symmetric_difference example
       2:  #include <iostream>     // std::cout
       3:  #include <algorithm>    // std::set_symmetric_difference, std::sort
       4:  #include <vector>       // std::vector
       5:   
       6:  int main () {
       7:    int first[] = {5,10,15,20,25};
       8:    int second[] = {50,40,30,20,10};
       9:    std::vector<int> v(10);                      // 0  0  0  0  0  0  0  0  0  0
      10:    std::vector<int>::iterator it;
      11:   
      12:    std::sort (first,first+5);     //  5 10 15 20 25
      13:    std::sort (second,second+5);   // 10 20 30 40 50
      14:   
      15:    it=std::set_symmetric_difference (first, first+5, second, second+5, v.begin());
      16:                                                 //  5 15 25 30 40 50  0  0  0  0
      17:    v.resize(it-v.begin());                      //  5 15 25 30 40 50
      18:   
      19:    std::cout << "The symmetric difference has " << (v.size()) << " elements:
    ";
      20:    for (it=v.begin(); it!=v.end(); ++it)
      21:      std::cout << ' ' << *it;
      22:    std::cout << '
    ';
      23:   
      24:    return 0;
      25:  }
  • 相关阅读:
    openstack-ntp时间同步服务
    如何将icon图标库引入自己的项目中
    微信小程序实现滑动tab切换和点击tab切换并显示相应的数据(附源代码)
    微信小程序分享至朋友圈的方法
    微信小程序--分享功能
    mpvue-新建页面、页面跳转、自适应单位
    微信小程序mpvue-动态改变navigationBarTitleText值
    mpvue中使用flyjs全局拦截
    H5 布局 -- 让容器充满屏幕高度或自适应剩余高度
    使用mpvue开发小程序如何定义全局变量
  • 原文地址:https://www.cnblogs.com/sosi/p/3660723.html
Copyright © 2011-2022 走看看