void deleteinfo()
{
string search ;
int found ;
cout << "\n Delete A Player's Information \n\n" ;
cout << "Please Enter The Player's Last Name : " ;
cin >> search ;
found=linsearch(search);
if (found==-1)
{
cout << "\n There is no player called " << search ;
}
else
{
player[found].getFirstName() = player[found + 1].getFirstName() ;
player[found].getLastName() = player[found + 1].getLastName() ;
player[found].getAge() == player[found + 1].getAge() ;
player[found].getCurrentTeam() = player[found + 1].getCurrentTeam() ;
player[found].getPosition() = player[found + 1].getPosition() ;
player[found].getStatus() = player[found + 1 ].getStatus() ;
cout << "\n Player has been deleted." ;
}
cin.get() ;
menu() ;
}
int linsearch(string val)
{
for (int j=0; j <= 3; j++)
{
if (player[j].getLastName()==val)
return j ;
}
return -1 ;
}
这只是一个如何解决此问题的示例。我假设您有一个静态长度数组(最大玩家数量)。
Player *Players[MAX_PLAYERS]; //Array with pointers to Player objects.
for(int i = 0; i < MAX_PLAYERS; ++i)
Players[i] = new Players(x, y, z); //Fills the array with some data.
现在进行擦除:
if(found > 0) {
delete Players[found]; //Destroys the object in question.
for(int i = found; i < MAX_PLAYERS - 1; ++i)
Players[i] = Players[i + 1]; //Moves the entire list up by one.
Players[MAX_PLAYERS - 1] = NULL; //Marks the new end of the list.
}