English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ The List sort() function arranges the elements of the given list in ascending order. It does not involve any construction or destruction of elements. Elements are only moved within the container.
void sort();
It does not contain any parameters.
It does not return any value.
Let's look at a simple example
#include iostream> #include<list> using namespace std; int main() { list<int> li={6,4,10,2,4,1}; list<int>::iterator itr; cout << "The elements of the list are:"; for(itr=li.begin();itr!=li.end();++itr) std::cout << *itr << ",", li.sort(); cout << '\n'; cout << "The sorted elements are:"; for(itr=li.begin();itr!=li.end();++itr) std::cout << *itr << ",", return 0; }
Output:
The elements of the list are: 6,4,10,2,4,1, The sorted elements are: 1,2,4,4,6,10
In this example, the sort() function sorts the elements of the given list, and its output is1,2,4,4,6,10.
Let's look at a simple example
#include iostream> #include<list> using namespace std; int main() { list<char> li={'n','h','o','o','o'}; list<char>:: iterator itr; for(itr=li.begin();itr!=li.end();++itr) std::cout << *itr; li.sort(); cout << '\n'; for(itr=li.begin();itr!=li.end();++itr) std::cout << *itr; return 0; }
Output:
w3codebox hnooo
In this example, the sort() function sorts the characters based on their ASCII values.