C++ Program to Implement Set_Difference in STL

«
»
This C++ Program demonstrates implementation of Set_Difference in STL.

Here is source code of the C++ Program to demonstrate Set_Difference 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_Difference 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_difference(first, first + 5, second, second + 5, v.begin());
  17.     v.resize(it - v.begin());
  18.     cout << "The difference has " << (v.size()) << " elements: "<<endl;
  19.     for (it = v.begin(); it != v.end(); ++it)
  20.         cout<< *it<<"  ";
  21.     cout <<endl;
  22.     return 0;
  23. }

advertisement
$ g++ set_Difference.cpp
$ a.out
The difference has 3 elements: 
5  15  25  
 
------------------
(program exited with code: 0)
Press return to continue

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

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn