Sort std::vector but ignore a certain number

前端 未结 6 618
我寻月下人不归
我寻月下人不归 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:20

    From your description, it appears that the score can be never negative. In that case, I'd recommend the scores to be a vector of unsigned int. You can define a constant

    const unsigned int INFINITY = -1;
    

    and load your vector with INFINITY initially. INFINITY is the maximum positive integer that can be stored in a 32 bit unsigned integer (which also corresponds to -1 in 2's complement)

    Then you could simply sort using

    sort(v.begin(),v.end());
    

    All INFINITY will be at the end after the sort.

提交回复
热议问题