Sort std::vector but ignore a certain number

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

    You can provide lambda as parameter for sort:

    std::sort(myVector.begin(), myVector.end(),[]( int i1, int i2 ) {
        if( i1 == -1 ) return false;
        if( i2 == -1 ) return true;
        return i1 < i2; }
    );
    

    here is the demo (copied from Kerrek)

    but it is not clear how you realize where is which score after sort.

提交回复
热议问题