Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q5. Using the Standard Template Library set class, write C++ functions that does

ID: 3734478 • Letter: Q

Question

Q5. Using the Standard Template Library set class, write C++ functions that does the following:

(a) Returns the union of two sets as a new STL set. For simplicity, let us assume we have a set of integers. If one set has the members {1, 3, 4} and another set has the members {1, 2, 4}, you need to create and return a new set with the members {1, 2, 3, 4}.

(b) Returns the intersection of two sets as a new STL set. If one set has the members {1, 3, 4} and another set has the members {1, 2, 4}, you need to create and return a new set with the members {1, 4}.

Explanation / Answer

I write both of the code in straight way. If you have ant doubt please write in the comment. If you satisfied please like the answer.

Answer a)

#include<iostream>
#include<algorithm> // STL Library
#include<vector> //STL Library
using namespace std;

//main function started

int main()
{

// Declaring and initalizing the first vector

vector<int> set1 = {1, 3, 4};

// Declaring and initalizing the second vector

vector<int> set2 = {1, 2, 4};

// output set
vector<int> set_output(4);

//set_union is used to find the the unoin of the sets and checks from begin
//to end for both the set and goto begining of the new set

auto it = set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), set_output.begin());

// output of the union into a variable from the new set and display
  
cout << "Union of two containers is : ";
for (int &output : set_output)
{
cout << output << " ";
}
cout<<endl;

}

Answer b)

#include<iostream>
#include<algorithm> // STL Library
#include<vector> //STL Library
using namespace std;

//main function started

int main()
{

// Declaring and initalizing the first vector

vector<int> set1 = {1, 3, 4};

// Declaring and initalizing the second vector

vector<int> set2 = {1, 2, 4};

// output set
vector<int> set_output(2);

//set_intersection is used to find the the intersection of the sets and checks from begin
//to end for both the set and goto begining of the new set

auto it = set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), set_output.begin());

// output of the intersection into a variable from the new set and display
  
cout << "intersection of two containers is : ";
for (int &output : set_output)
{
cout << output << " ";
}
cout<<endl;

}