Wednesday 11 May 2011

Example of set_union and set_intersection

Program as follows:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace
std;

int
main()
{

int
setOne[] = {5, 10, 15, 20, 25};
int
setTwo[] = {50, 40, 30, 20, 10, 11, 21, 31, 41, 51};

int
setOneSize = sizeof(setOne) / sizeof(int);
int
setTwoSize = sizeof(setTwo) / sizeof(int);

//Its necessary to sort if not already sorted
sort(setTwo, setTwo + setTwoSize);

vector<int> unionSetVector(setOneSize + setTwoSize);
set_union(setOne, setOne + setOneSize, setTwo, setTwo + setTwoSize, unionSetVector.begin());

cout<<"\n1. unionSetVector : ";
copy(unionSetVector.begin(), unionSetVector.end(), ostream_iterator<int>(cout, " "));
cout<<endl;

vector<int> intersectionSetVector(min(setOneSize, setTwoSize));
set_intersection(setOne, setOne + setOneSize, setTwo, setTwo + setTwoSize, intersectionSetVector.begin());

cout<<"\n1. intersectionSetVector : ";
copy(intersectionSetVector.begin(), intersectionSetVector.end(), ostream_iterator<int>(cout, " "));
cout<<endl;

cout<<endl;
return
0;
}


The output is as follows:

2 comments:

  1. How does one find the length of union set? Here the union set has a length of 13. How do we get that?

    ReplyDelete
  2. I guess you mean the size of unionSetVector. Check out the BasicVectorManipulation example

    ReplyDelete