Use std::sort to find top N items in a std::vector

c++, sorting, stl

Solution

Try `std::partial_sort` instead of `std::sort`. :)

Problem

I need to sort the elements in a `std::vector`, but I'm only interested in the top `N` items being sorted, not the entire list: E.g. in a list of 10 elements, only first 3 have to be sorted. Don't care about the rest... 1,2,3,6,7,4,9,8,5 Can this be done using `std::sort`? Edit I simply needed to find the top `N` items in a vector. `std::partial_sort_copy` was exactely what I needed.

Original source

Related problems