Friday, 24 November 2017

C++ Program to Implement Sorting containers in STL


Code:

#include    iostream
#include    list
#include    cstdlib
using namespace std;
int main()
{
    int choice, item;
    list lt;
    list::iterator it;
    while (1)
    {
        cout<<"\n---------------------"<
        cout<<"Sorting Containers Implementation in Stl"<
        cout<<"\n---------------------"<
        cout<<"1.Insert Element into the List"<
        cout<<"2.Display Sorted Elements"<
        cout<<"3.Exit"<
        cout<<"Enter your Choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter the element to be inserted: ";
            cin>>item;
            lt.push_back(item);
            break;
        case 2:
            lt.sort();
            cout<<"Elements of Sorted List: ";
            for (it = lt.begin(); it != lt.end(); ++it)
                cout <<"  "<< *it;
            cout << endl;
            break;
        case 3:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<
        }
    }
    return 0;
}


Output:

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 1
Enter the element to be inserted: 9

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 1
Enter the element to be inserted: 3

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 1
Enter the element to be inserted: 5

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 1
Enter the element to be inserted: 6

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 1
Enter the element to be inserted: 2

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 1
Enter the element to be inserted: 5

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 2
Elements of Sorted List:   2  3  5  5  6  9

---------------------
Sorting Containers Implementation in Stl

---------------------
1.Insert Element into the List
2.Display Sorted Elements
8.Exit
Enter your Choice: 3


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