Sort std::vector but ignore a certain number

前端 未结 6 671
我寻月下人不归
我寻月下人不归 2021-01-28 12:05

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

6条回答
  •  我在风中等你
    2021-01-28 12:28

    Combine partitioning and sorting:

    std::sort(v.begin(),
              std::partition(v.begin(), v.end(), [](int n){ return n != -1; }));
    

    If you store the iterator returned from partition, you already have a complete description of the range of non-trivial values, so you don't need to look for −1s later.

提交回复
热议问题