I have an std::vector
of the size 10 and each entry is initially -1. This vector represents a leaderboard for my game (high scores), and -1 just means th
std::sort
supports using your own comparison function with the signature bool cmp(const T& a, const T& b);
. So write your own function similar to this:
bool sort_negatives(const int& a, const int& b)
{
if (a == -1) {
return false;
}
if (b == -1) {
return true;
}
return a < b;
}
And then call sort
like std::sort(myVector.begin(), myVector.end(), sort_negatives);
.
EDIT: Fixed the logic courtesy of Slava. If you are using a compiler with C++11 support, use the lambda or partition answers, but this should work on compilers pre C++11.