Howto Sort a Vector or a List in C++ using STL
A little code snippet that people need very often.
/*
* Howto sort a vector or a list in C++ using STL
*/
using namespace std; // Save us some typing
/*
* This is a comparison function. It can be used to tell sort()
* how to order the elements in our container (the vector or list).
* You can write a comparator for every data type (i.e. double, string...).
*/
bool
int
Compilation and execution
Save the above code inside a file, e.g. list_vector.cpp
and compile it like so:
clang++ list_vector.cpp
To run it, execute the resulting binary.
./a.out
Program output
Vector: 12 5 1
List: 12 5 1
Thanks for reading! I mostly write about Rust and my (open-source) projects. If you would like to receive future posts automatically, you can subscribe via RSS.