I want to have a stl list of objects where each object contains two int‘s.
Afterwards I want to sort the list with stl::sort after the value of the first int.
How do I tell the sort function that it’s supposed to sort after the first int?
I want to have a stl list of objects where each object contains two
Share
You can specify a custom sort predicate. In C++11 this is best done with a lambda:
In older versions of C++ you have to write an appropriate function:
(Instead if
ipairyou can of course have your own data structure; just modify the comparison function accordingly to access the relevant data member.)Finally, if this makes sense, you can also equip your custom class with an
operator<. That allows you to use the class freely in any ordered context, but be sure to understand the consequences of that.