Friday, 24 November 2017

C++ Program to Implement Set in STL


Code:

#include     iostream
#include     set
#include     string
#include     cstdlib
using namespace std;
int main()
{
    set st;
    set::iterator it;
    int choice, item;
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"Set Implementation in Stl"<
        cout<<"\n---------------------"<
        cout<<"1.Insert Element into the Set"<
        cout<<"2.Delete Element of the Set"<
        cout<<"3.Size of the Set"<
        cout<<"4.Find Element in a Set"<
        cout<<"5.Dislplay by Iterator"<
        cout<<"6.Exit"<
        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<
            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" <
            else
                cout<<"No Element Found"<
            break;
        case 5:
            cout<<"Displaying Map by Iterator: ";
            for (it = st.begin(); it != st.end(); it++)
            {
                cout << (*it)<<" ";
            }
            cout<
            break;
        case 6:
            exit(1);
    break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

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



More C++ Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...