删除存储在数组中的特定类对象 [英] Deleting specific class objects stored in an array

查看:76
本文介绍了删除存储在数组中的特定类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题,我想知道是否有人可以看一下,我有一个创建的函数可以从数组中删除特定元素.我使用线性搜索来查找元素,然后用之后的元素覆盖要删除的元素,因为我还没有找到一种专门删除元素的方法.我的问题是代码不会真正起作用,因为元素不会被覆盖,一旦元素被覆盖,还有没有办法在数组中保留空白.

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.

下面是我的代码:

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

推荐答案

这只是一个示例,您可能如何解决此问题.我假设您有一个静态长度数组(最大玩家数).

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.

现在要擦除:

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

当您遇到第一个NULL指针(并且最晚是MAX_PLAYERS)时,数组在它的末端",这说明了您的空白".或者,您可以省略向上移动",而只需销毁对象并将指针设置为NULL.这样,您就会知道那里没有玩家.

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.

这篇关于删除存储在数组中的特定类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆