This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Common set algorithm

【 Introduction 】 :

  •     set_intersection; // Find the intersection of two containers
  •     set_union; // Find the union of two containers
  •     set_difference; // Find the difference between the two containers

set_intersection

【 Function 】 :

Find the intersection of elements in two containersCopy the code

Function prototype:

set_intersection(iterator begin1,iterator end1,iterator begin2, iterator end3,iterator dest);

// Select ** from * / The elements of both containers must be ordered // begin1 the beginning iterator of container 1 // end1 the end iterator of container 1 // begin2 the beginning iterator of container 2 // end2 the end iterator of container 2 // dest The start iterator of the target containerCopy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class MyPrint
{
public:
    void operator(a)(int v)
    {
        cout << v << ""; }};void test01(a)
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    vector<int> v2;
    for (int i = 5; i < 15; i++)
    {

        v2.push_back(i);
    }

    // cout << "v1:";
    // for_each(v1.begin(), v1.end(), MyPrint());
    // cout << endl;

    // cout << "place the intersection elements in v1: ";
    // set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v1.begin());
    // for_each(v1.begin(), v1.end(), MyPrint());
    // cout << endl;

    vector<int> vTarget;
    // Take the size of the smaller of the two containers (default 0 for excess space)
    vTarget.resize(min(v1.size(), v2.size()));
    vector<int>::iterator pos = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    // cout << *pos << endl; // The return value is the position of the last element in the intersection
    cout << "The intersection of v1 and v2 is:";
    for_each(vTarget.begin(), vTarget.end(), MyPrint());
    cout << endl;
}
int main(a)
{
    test01(a);return 0;
}
Copy the code

Summary:

  • The two sets that intersect must be ordered sequences
  • The target container makes space for the smaller of the two containerssize
  •     set_intersetctionThe return value is the position of the last element in the intersection

set_union

【 Function 】 :

Find the union of the elements in two containersCopy the code

Function prototype:

set_union(iterator begin1,iterator end1,iterator begin2,iterator end2, iterator dest);

// Select ** from * /; The elements of both containers must be ordered // begin1 the beginning iterator of container 1 // end1 the end iterator of container 1 // begin2 the beginning iterator of container 2 // end2 the end iterator of container 2 // dest The start iterator of the target containerCopy the code

“Demo” :

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class MyPrint
{
public:
    void operator(a)(int v)
    {
        cout << v << ""; }};void test01(a)
{
    vector<int> v1;
    for (int i = 0; i < 5; i++)
    {
        v1.push_back(i);
    }

    vector<int> v2;
    for (int i = 5; i < 10; i++)
    {

        v2.push_back(i);
    }

    vector<int> vTarget;
    // Create space for the target container, take the sum of the sizes of the two containers
    vTarget.resize(v1.size() + v2.size());
    set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

    for_each(vTarget.begin(), vTarget.end(), MyPrint());
    cout << endl;
}
int main(a)
{
    test01(a);return 0;
}
Copy the code

Summary:

  • The two container elements of the union must be ordered
  • The target container makes space and takes two containerssizeThe sum of the
  •     set_unionThe return value is the position of the last element in the union set

set_difference

【 Function 】 :

Find the difference set of elements in two containersCopy the code

Function prototype:

set_difference(iterator begin1,iterator end1,iterator begin2,iterator end2,iterator dest);

// ** ** ** * : The elements of both containers must be ordered // begin1 the beginning iterator of container 1 // end1 the end iterator of container 1 // begin2 the beginning iterator of container 2 // end2 the end iterator of container 2 // dest The start iterator of the target containerCopy the code

“Demo” :

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class MyPrint
{
public:
    void operator(a)(int v)
    {
        cout << v << ""; }};void test01(a)
{
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    vector<int> v2;
    for (int i = 5; i < 15; i++)
    {

        v2.push_back(i);
    }

    cout << "The difference set of v1 and v2 is:;
    vector<int> vTarget1;
    // Make space for the target container, take the size of the larger container
    vTarget1.resize(max(v1.size(), v2.size()));
    set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget1.begin());
    for_each(vTarget1.begin(), vTarget1.end(), MyPrint());
    cout << endl;

    cout << The difference set between V2 and v1 is:;
    vector<int> vTarget2;
    // Make space for the target container, take the size of the larger container
    vTarget2.resize(max(v1.size(), v2.size()));
    set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget2.begin());
    for_each(vTarget2.begin(), vTarget2.end(), MyPrint());
    cout << endl;
}

int main(a)
{
    test01(a);return 0;
}
Copy the code

Summary:

  • The two sets of difference sets must be ordered sequences
  • The target container makes room for the larger of the two containerssizevalue
  •     set_differenceThe return value is the position of the last element in the difference set

“Welcome to the discussion in the comments section. The nuggets will be giving away 100 nuggets in the comments section after the diggnation project. See the event article for details.”