c++ - Sort std::vector<int> but ignore a certain number -
i have std::vector<int> of size 10 , each entry -1. vector represents leaderboard game (high scores), , -1 means there no score entry.
std::vector<int> myvector; myvector.resize(10, -1); when game started, want load high score file. load each line (up 10 lines), convert value found int std::stoi, , if number >0 replace -1 in vector @ current position.
all works. problem:
since values in file aren't sorted, want sort myvector after load entries. with
std::sort(myvector.begin(), myvector.end()); this sorts in ascending order (lower score better in game).
the problem that, since vector filled -1 , there aren't 10 entries saved in high scores file, vector might contain few -1 in addition player's scores.
that means when sorting vector above code, -1 appear before player's scores.
my question is: how sort vector (in ascending order), entries -1 put @ end (since don't represent real score)?
combine partitioning , sorting:
std::sort(v.begin(), std::partition(v.begin(), v.end(), [](int n){ return n != -1; })); if store iterator returned partition, have complete description of range of non-trivial values, don't need −1s later.
Comments
Post a Comment