Sorting Containers
(1) void sort(); (2) template <class Compare> void sort (Compare comp);
Sort elements in container:
Sorts the elements in the list, altering their position within the container.
The sorting is performed by applying an algorithm that uses either operator< (in version (1)) or comp (in version (2)) to compare elements. This comparison shall produce a strict weak ordering of the elements (i.e., a consistent transitive comparison, without considering its reflexiveness).
The resulting order of equivalent elements is stable: i.e., equivalent elements preserve the relative order they had before the call.
The entire operation does not involve the construction, destruction or copy of any element object. Elements are moved within the container.
C++ Program for Sorting Containers Implementation in STL :
#include <iostream> #include <list> #include <cstdlib> using namespace std; int main() { int choice, item; list<int> lt; list<int>::iterator it; while (1) { cout<<"n---------------------"<<endl; cout<<"Sorting Containers Implementation in Stl"<<endl; cout<<"n---------------------"<<endl; cout<<"1.Insert Element into the List"<<endl; cout<<"2.Display Sorted Elements"<<endl; cout<<"3.Exit"<<endl; 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"<<endl; } } return 0; }
Sample 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 ------------------