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. }

advertisement
$ 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
If you wish to look at all C++ Programming examples, go to C++ Programs.

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