美文网首页
21.set_difference求差集

21.set_difference求差集

作者: lxr_ | 来源:发表于2021-05-18 17:11 被阅读0次
    #include<iostream>
    using namespace std;
    
    #include<vector>
    #include<algorithm>
    
    //v1与v2的差集   不同于    v2与v1的差集
    
    void test2101()
    {
        vector<int> v1, v2;
        vector<int> vTarget;
    
        for (int i = 0; i < 10; i++)
        {
            v1.push_back(i);
            v2.push_back(i + 5);
        }
    
        vTarget.resize(max(v1.size(), v2.size()));
    
        //v1与v2的差集:
        vector<int>::iterator itEnd1 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
        for (vector<int>::iterator it = vTarget.begin(); it != itEnd1; it++)
        {
            cout << (*it) << " ";
        }
        cout << endl;
    
        //v2与v1的差集:
        vector<int>::iterator itEnd2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
        for (vector<int>::iterator it = vTarget.begin(); it != itEnd2; it++)
        {
            cout << (*it) << " ";
        }
        cout << endl;
    }
    int main()
    {
    
        test2101();
    
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:21.set_difference求差集

          本文链接:https://www.haomeiwen.com/subject/vbgirltx.html