c++ - partial sort a vector with lambda predicate -
i partial sort vector predicate such
std::vector<std::pair<std::string, int>> vp; std::partial_sort(vp.begin(), vp.begin()+10, [](const std::pair<std::string,int> &left, const std::pair<std::string,int> &right) { return left.second > right.second; });
however error
no matching function call ‘partial_sort(std::vector<std::pair<std::basic_string<char>, int> >::iterator, __gnu_cxx::__normal_iterator<std::pair<std::basic_string<char>, int>*, ....
the above works fine std::sort , not partial_sort
suggestions ?
looking @ reference documentation, find std::partial_sort
requires 3 iterators, not 2: start, middle , end. re-arrange range range [start, middle)
sorted, , contains smallest elements range [start, end)
.
depending on you're trying achieve, need provide appropriate 3rd iterator. if you're trying find 10 smallest elements, this:
std::partial_sort(vp.begin(), vp.begin()+10, vp.end(), /*lambda before*/);
Comments
Post a Comment