1

I would like to partial sort this vector with a predicate as 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 I get the error

 no matching function for call to ‘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 for std::sort and not for partial_sort any suggestions ?

2
  • Care to provde the ...? Does it list candidate functions considered and reasons why they were not applicable? Commented Dec 2, 2014 at 8:03
  • partial_sort requires three iterators? (I only see two.) Commented Dec 2, 2014 at 8:05

1 Answer 1

4

Looking at some reference documentation, you will find that std::partial_sort requires 3 iterators, not 2: start, middle and end. It will re-arrange the range so that the range [start, middle) is sorted, and contains the smallest elements from the range [start, end).

Depending on what exactly you're trying to achieve, you need to provide an appropriate 3rd iterator. If you're trying to find the 10 smallest elements, just do this:

std::partial_sort(vp.begin(), vp.begin()+10, vp.end(), /*lambda as before*/);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.