C++ Program to Implement Set in STL

This C++ Program demonstrates implementation of Set in Stl.

Here is source code of the C++ Program to demonstrate Set 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 in Stl
  3.  */
  4. #include <iostream>
  5. #include <set>
  6. #include <string>
  7. #include <cstdlib>
  8. using namespace std;
  9. int main()
  10. {
  11.     set<int> st;
  12.     set<int>::iterator it;
  13.     int choice, item;
  14.     while (1)
  15.     {
  16.         cout<<"\n---------------------"<<endl;
  17.         cout<<"Set Implementation in Stl"<<endl;
  18.         cout<<"\n---------------------"<<endl;
  19.         cout<<"1.Insert Element into the Set"<<endl;
  20.         cout<<"2.Delete Element of the Set"<<endl;
  21.         cout<<"3.Size of the Set"<<endl;
  22.         cout<<"4.Find Element in a Set"<<endl;
  23.         cout<<"5.Dislplay by Iterator"<<endl;
  24.         cout<<"6.Exit"<<endl;
  25.         cout<<"Enter your Choice: ";
  26.         cin>>choice;
  27.         switch(choice)
  28.         {
  29.         case 1:
  30.             cout<<"Enter value to be inserted: ";
  31.             cin>>item;
  32.             st.insert(item);
  33.             break;
  34.         case 2:
  35.             cout<<"Enter the element to be deleted: ";
  36.             cin>>item;
  37.             st.erase(item);
  38.             break;
  39.         case 3:
  40.             cout<<"Size of the Set: ";
  41.             cout<<st.size()<<endl;
  42.             break;
  43.         case 4:
  44. 	    cout<<"Enter the element to be found: ";
  45. 	    cin>>item;
  46.             it = st.find(item);
  47. 	    if (it != st.end())
  48.                 cout<<"Element "<<*it<<" found in the set" <<endl;
  49.             else
  50.                 cout<<"No Element Found"<<endl;
  51.             break;
  52.         case 5:
  53.             cout<<"Displaying Map by Iterator: ";
  54.             for (it = st.begin(); it != st.end(); it++)
  55.             {
  56.                 cout << (*it)<<" ";
  57.             }
  58.             cout<<endl;
  59.             break;
  60.         case 6:
  61.             exit(1);
  62. 	    break;
  63.         default:
  64.             cout<<"Wrong Choice"<<endl;
  65.         }
  66.     }
  67.     return 0;
  68. }

$ g++ set.cpp
$ a.out
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 1
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 2
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 3
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 4
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 5
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 4
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 3
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 2
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 1
Enter value to be inserted: 1
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 3
Size of the Set: 5
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 5
Displaying Map by Iterator: 1 2 3 4 5 
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 4
Enter the element to be found: 3
Element 3 found in the set
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 2
Enter the element to be deleted: 5
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 3
Size of the Set: 4
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 5
Displaying Map by Iterator: 1 2 3 4 
 
---------------------
Set Implementation in Stl
 
---------------------
1.Insert Element into the Set
2.Delete Element of the Set
3.Size of the Set
4.Find Element in a Set
5.Dislplay by Iterator
6.Exit
Enter your Choice: 6
 
 
------------------
(program exited with code: 1)
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.

If you find any mistake above, kindly email to [email protected]

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.