- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Strand sort in C++
In this section we will see how we can sort some array or linked list using standard library of C++. In C++ there are multiple different libraries that can be used for different purposes. The sorting is one of them.
The C++ function std::list::sort() sorts the elements of the list in ascending order. The order of equal elements is preserved. It uses operator< for comparison.
Example
#include <iostream> #include <list> using namespace std; int main(void) { list<int> l = {1, 4, 2, 5, 3}; cout << "Contents of list before sort operation" << endl; for (auto it = l.begin(); it != l.end(); ++it) cout << *it << endl; l.sort(); cout << "Contents of list after sort operation" << endl; for (auto it = l.begin(); it != l.end(); ++it) cout << *it << endl; return 0; }
Output
Contents of list before sort operation 1 4 2 5 3 Contents of list after sort operation 1 2 3 4 5
Advertisements