Deleting specific class objects stored in an array

2019-02-26 00:17发布

问题:

I have a problem with my code and I was wondering if someone could have a look, I have a function I have created to delete a specific element from an array. I use a linear search to find the element then I overwrite the element I want to get rid of with the one after as I have not found a way to delete an element specifically. my problem is that the code does not really work as the element does not get overwritten, also is there a way to leave an blank space in the array once the element has been overwritten.

below is my code:

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 ;
}

回答1:

This is merely an example how you may be able to solve this problem. I'm assuming, that you have a static length array (maximum number of players).

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.

Now for your erasing:

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.
}

This little snippet will not 'copy' the entire object, but rather move them up in the array (without reconstructing any object).

The array is at it's 'end', when you encounter the first NULL pointer (and at MAX_PLAYERS latest), which accounts for your 'blank space'. Alternatively, you can omit the 'moving up' and just destroy the object and set the pointer to NULL. That way, you'll know, that there's no player there.



回答2:

What you have to do is copy all elements that follow the element you want to delete by one position to the left and at end update the new length of the array. For example:

for (size_t i = found + 1; i < player_length; ++i) {
    player[i - 1] = player[i];
}
--player_length;

The objects in your player array must be copyable. I assume you have a variable somewhere that holds the current length of the array ("length" as in how many players it currently has in it, not its total capacity.)