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

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

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