简体   繁体   中英

C++ Dynamic Array of Objects Sorting by Property

I'm trying to implement selection sort as a member function within class, to sort the objects of the class where the number of total players are being got by user input, also the names and scores of the players are being got by user too.

I'll sort the player objects by the property of their scores, which is a class member, being got by user input.

My problem is, i got stuck within the main where i can't call the class' member function sort for the array of objects.

class Player{
private:
string name;
int score;

public:
void setStatistics(string, int) // simple setter, not writing the whole function
void sortPrint(int, Player []);
int getScore(){ return score; }
void print(){ cout << name << " " << score << endl; }
};

void Player::sortPrint(int n, Player arr[]){
int i, j, minIndex;
Player tmp;

for (i = 0; i < n - 1; i++) {
    int maxIndex = i;
    for (j = i + 1; j < n; j++) 
    {
          if (arr[j].getScore() > arr[minIndex].getScore())
          {
                minIndex = j;
          }
    }

    if (minIndex != i) {
          tmp = arr[i];
          arr[i] = arr[minIndex];
          arr[minIndex] = tmp;
    }

for(int i=0; i<n; i++){
arr[i].print(); // not sure with this too
}

}

};

int main(){
int n,score;
string name;

cout << "How many players ?" << endl;
cin >> n;

Player **players;
players = new Player*[n]; 

for(int i=0;i<n;i++) {

cout << "Player's name :" << endl;
cin >> name;
cout << "Player's total score:" << endl;
cin >> score;
players[i] = new Player;
players[i]->setStatistics(name,score); 

}

for(int i=0; i<n;i++){
players->sortPrint(n, players); // error here, dont know how to do this part
}

// returning the memory here, didn't write this part too.

}

尝试将void Player::sortPrint(int n, Player arr[])替换为void Player::sortPrint(int n, Player*)并调用players->sortPrint(n, *players)函数

Your problem is, that players is a pointer to array of Player , and arrays do not have member functions of the containees. As Player::sortPrint does not depend on the object itself, declare it as static and call it like Player::sortPrint(n, players);

Unless you have a very good reason not to, you should use std::sort rather than your own sorting algorithm. You should use a comparison function which compares the score of each player.

The following should work in C++03:

bool comparePlayerScores(const Player* a, const player* b)
{
    return (a->getScore() < b->getScore());
}


// Returns the players sorted by score, in a new std::vector
std::vector<Player*> getSortedPlayers(Player **players, int num_players)
{
    std::vector<Player*> players_copy(players, players + num_players);
    std::sort(players_copy.begin(), players_copy.end(), comparePlayerScores);
    return players_copy;
}

void printSorted(Player **players, int num_players)
{
    std::vector<Player*> sorted_players = getSortedPlayers(players, num_players);
    // Could use iterators here, omitting for brevity
    for (int i = 0; i < num_players; i++) {
        sorted_players[i]->print();
    }
}

(Alternatively, you could define an operator< on your Player class which compares scores, which would let you store players in a std::set or std::map .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM