zoukankan      html  css  js  c++  java
  • c++STL之常用集合算法

    set_intersection:求两个容器的交集

    set_union:求两个集合的并集

    set_difference:求两个集合的差集

    1.set_intersection

    #include<iostream>
    using namespace std;
    #include <vector>
    #include <algorithm>
    
    //常用集合算法 set_intersection
    void myPrint(int val)
    {
        cout << val << " ";
    }
    
    void test01()
    {
        vector<int>v1;
        vector<int>v2;
        for (int i = 0; i < 10; i++)
        {
            v1.push_back(i);   // 0 ~ 9
            v2.push_back(i + 5);  // 5 ~ 14
        }
    
        vector<int>vTarget;
        //目标容器需要提前开辟空间
        //最特殊情况 大容器包含小容器  开辟空间 取小容器的size即可
        vTarget.resize(min(v1.size(), v2.size()));
    
        //获取交集
        vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    
        for_each(vTarget.begin(), itEnd, myPrint);
        cout << endl;
    }
    
    int main() {
    
        test01();
    
        system("pause");
    
        return 0;
    }

    2.set_union

    #include<iostream>
    using namespace std;
    #include <vector>
    #include <algorithm>
    //常用集合算法 set_union
    void myPrint(int val)
    {
        cout << val << " ";
    }
    
    void test01()
    {
        vector<int>v1;
        vector<int>v2;
    
        for (int i = 0; i < 10; i++)
        {
            v1.push_back(i);
            v2.push_back(i + 5);
        }
    
        vector<int>vTarget;
        //目标容器提前开辟空间
        //最特殊情况 两个容器没有交集,并集就是两个容器size相加
        vTarget.resize(v1.size() + v2.size());
    
        vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    
        for_each(vTarget.begin(), itEnd, myPrint);
        cout << endl;
    }
    
    int main() {
    
        test01();
    
        system("pause");
    
        return 0;
    }

    3.set_difference

    #include<iostream>
    using namespace std;
    #include <vector>
    #include <algorithm>
    
    void myPrint(int val)
    {
        cout << val << " ";
    }
    
    //常用集合算法  set_difference 
    void test01()
    {
        vector<int>v1;
        vector<int>v2;
        for (int i = 0; i < 10; i++)
        {
            v1.push_back(i);
            v2.push_back(i+5);
        }
    
        //创建目标容器
        vector<int>vTarget;
        //给目标容器开辟空间
        //最特殊情况  两个容器没有交集 取两个容器中大的size作为目标容器开辟空间
        vTarget.resize( max(v1.size(),v2.size()) );
    
        cout << "v1和v2的差集为:" << endl;
    
        vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    
        for_each(vTarget.begin(), itEnd, myPrint);
        cout << endl;
    
        cout << "v2和v1的差集为:" << endl;
        itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
    
        for_each(vTarget.begin(), itEnd, myPrint);
        cout << endl;
    }
    
    int main() {
    
        test01();
    
        system("pause");
    
        return 0;
    }
  • 相关阅读:
    GLSL预定义变量
    GLSL 内建函数
    GLSL语言基础
    svn:revert to this version 和 revert changes from this version的区别
    win7下搭建opengles2.0编程环境
    iconv字符编码转换
    矩阵-DirectX与OpenGL的不同
    NHibernate分页
    Web网站压力测试工具
    winform系统自动登录实现
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12114800.html
Copyright © 2011-2022 走看看