C++ Program to Implement Set_Union in STL

This C++ Program demonstrates implementation of Set_Union in STL.

Here is source code of the C++ Program to demonstrate Set_Union in STL. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Set_Union in Stl
  3.  */
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <vector>
  7. using namespace std;
  8. int main ()
  9. {
  10.     int first[] = {5,10,15,20,25};
  11.     int second[] = {50,40,30,20,10};
  12.     vector<int> v(10);
  13.     vector<int>::iterator it;
  14.     sort (first, first + 5);
  15.     sort (second, second + 5);
  16.     it = set_union (first, first + 5, second, second + 5, v.begin());
  17.     v.resize(it - v.begin());
  18.     cout << "The union has " << (v.size()) << " elements: "<<endl;
  19.     for (it = v.begin(); it != v.end(); ++it)
  20.         cout<< *it<<"  ";
  21.     cout <<endl;
  22.     return 0;
  23. }

$ g++ set_union.cpp
$ a.out
The union has 8 elements: 
5  10  15  20  25  30  40  50  
 
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.