C++ Program to Implement Sorting Containers in STL

This C++ Program demonstrates implementation of Sorting containers in STL.

Here is source code of the C++ Program to demonstrate Sorting containers 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 Sorting containers in Stl
  3.  */
  4. #include <iostream>
  5. #include <list>
  6. #include <cstdlib>
  7. using namespace std;
  8. int main()
  9. {
  10.     int choice, item;
  11.     list<int> lt;
  12.     list<int>::iterator it;
  13.     while (1)
  14.     {
  15.         cout<<"\n---------------------"<<endl;
  16.         cout<<"Sorting Containers Implementation in Stl"<<endl;
  17.         cout<<"\n---------------------"<<endl;
  18.         cout<<"1.Insert Element into the List"<<endl;
  19.         cout<<"2.Display Sorted Elements"<<endl;
  20.         cout<<"3.Exit"<<endl;
  21.         cout<<"Enter your Choice: ";
  22.         cin>>choice;
  23.         switch(choice)
  24.         {
  25.         case 1:
  26.             cout<<"Enter the element to be inserted: ";
  27.             cin>>item;
  28.             lt.push_back(item);
  29.             break;
  30.         case 2:
  31.             lt.sort();
  32.             cout<<"Elements of Sorted List: ";
  33.             for (it = lt.begin(); it != lt.end(); ++it)
  34.                 cout <<"  "<< *it;
  35.             cout << endl;
  36.             break;
  37.         case 3:
  38.             exit(1);
  39.             break;
  40.         default:
  41.             cout<<"Wrong Choice"<<endl;
  42.         }
  43.     }
  44.     return 0;
  45. }

$ g++ sorting_container.cpp
$ a.out
---------------------
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

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.