1

Im currently learning c++ and directx 9. I am creating a small game which I want to have a high score system, I have the reading and writing of the files done, I am just stuck on how to sort and insert a new value into my score vector.

My method for reading files into a vector is below:

     vector<int> Highscore::readFile()
     {
      int score;
       highScoreIn.open ("Highscore.txt", ios::out | ios::binary);
       if (highScoreIn.is_open())
       {

       while(highScoreIn>>score)
         {
           scores.push_back(score);
         }
         highScoreIn.close();
    return(scores);
       }
       else cout << "Unable to open file"; 
     }

I want a function that will check an integer passed into the function against the 5 values I am storing in the vector scores, then insert it into the right place in the vector. Any help on this would be appreciated:)

4

1 回答 1

2

假设您的向量从最大到最小排序

bool insert( vector<int> &v, int n ) {
    for ( auto it = v.begin(); it != v.end(), ++it ) {
        if ( *it < n ) {
            v.insert( it, n );
            v.pop_back()
            return true;
        }
     }
     return false;
}
于 2013-05-08T16:51:40.990 回答