c ++ STL collections of commonly used algorithm

set_intersection: the intersection of two containers

set_union: find two sets of set and

set_difference: differencing the two sets set

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;
     // target container need to open up the space in advance
     @ most special case of a large container vessel containing a small open space to take a small container size can 
    vTarget.resize (min (v1.size (), v2.size ())); 

    // Get the intersection 
    Vector < int > :: = Iterator itEnd set_intersection (v1.begin (), v1.end (), v2.begin (), v2.end (), vTarget.begin ()); 

    the for_each (vTarget.begin (), itEnd, the 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;
     // target container open space ahead
     @ most exceptional circumstances, there is no intersection of two containers, two containers size is set and the sum 
    vTarget.resize (v1.size ( ) + v2.size ()); 

    Vector < int > :: = Iterator itEnd set_union (v1.begin (), v1.end (), v2.begin (), v2.end (), vTarget.begin ()) ; 

    the for_each (vTarget.begin (), itEnd, the 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 ); 
    } 

    // Create a target container 
    Vector < int > Vtarget;
     // to the target container open space
     // most exceptional circumstances, taking the intersection of two containers without a large size two containers open as the target container space 
    vTarget.resize (max (v1.size (), v2.size ())); 

    COUT << " V1 and v2 difference set as: " << endl; 

    Vector < int > :: = Iterator itEnd set_difference (V1 .begin (), v1.end (), v2.begin (), v2.end (), vTarget.begin ()); 

    the for_each (vTarget.begin (), itEnd, the 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;
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/12114800.html