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.

`/*`

`* C++ Program to Implement Set in Stl`

`*/`

`#include <iostream>`

`#include <set>`

`#include <string>`

`#include <cstdlib>`

using namespace std;

int main()

`{`

set<int> st;

set<int>::iterator it;

int choice, item;

while (1)

`{`

cout<<"\n---------------------"<<endl;

cout<<"Set Implementation in Stl"<<endl;

cout<<"\n---------------------"<<endl;

cout<<"1.Insert Element into the Set"<<endl;

cout<<"2.Delete Element of the Set"<<endl;

cout<<"3.Size of the Set"<<endl;

cout<<"4.Find Element in a Set"<<endl;

cout<<"5.Dislplay by Iterator"<<endl;

cout<<"6.Exit"<<endl;

cout<<"Enter your Choice: ";

cin>>choice;

switch(choice)

`{`

case 1:

cout<<"Enter value to be inserted: ";

cin>>item;

st.insert(item);

break;

case 2:

cout<<"Enter the element to be deleted: ";

cin>>item;

st.erase(item);

break;

case 3:

cout<<"Size of the Set: ";

cout<<st.size()<<endl;

break;

case 4:

cout<<"Enter the element to be found: ";

cin>>item;

it = st.find(item);

if (it != st.end())

cout<<"Element "<<*it<<" found in the set" <<endl;

`else`

cout<<"No Element Found"<<endl;

break;

case 5:

cout<<"Displaying Map by Iterator: ";

for (it = st.begin(); it != st.end(); it++)

`{`

cout << (*it)<<" ";

`}`

cout<<endl;

break;

case 6:

exit(1);

break;

default:

cout<<"Wrong Choice"<<endl;

`}`

`}`

return 0;

`}`

$ 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.**

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