删除阵列中的strored特定的类对象 [英] Deleting specific class objects strored in an array

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

问题描述

我有我的code的一个问题,我想知道,如果有人可以看看,我有我创建从一个阵列中删除了某个元素的功能。我使用线性搜索来查找元素,然后我覆盖,我想摆脱与一前一后,因为我还没有找到一种方法来明确删除元素的元素。我的问题是,code并未真正发挥作用的元素没有被覆盖,也就是有办法在阵列中留下空白,一旦元素已被覆盖。

感谢

下面是我的code:

 无效deleteinfo()
{
    字符串搜索;
    INT找到;    COUT<< \\ n删除玩家的资料\\ n \\ n;
    COUT<< 请输入球员的姓氏:
    CIN>>搜索;    找到= linsearch(搜索);    如果(发现== - 1)
    {
        COUT<< \\ n没有球员被称为<<搜索;
    }
    其他
    {
        播放器[发现] .getFirstName()=播放器[找到+ 1] .getFirstName();
        播放器[发现] .getLastName()=播放器[找到+ 1] .getLastName();
        播放器[发现] .getAge()==播放器[找到+ 1] .getAge();
        播放器[发现] .getCurrentTeam()=播放器[找到+ 1] .getCurrentTeam();
        播放器[发现] .getPosition()=播放器[找到+ 1] .getPosition();
        播放器[发现] .getStatus()=播放器[找到+ 1] .getStatus();        COUT<< \\ N播放器已被删除。 ;
    }    cin.get();    菜单();
}
INT linsearch(字符串VAL)
{
    对于(INT J = 0; J< = 3; J ++)
    {
        如果(播放器[J] .getLastName()== VAL)
         复位J;
    }
        返回-1;
}


解决方案

这只是一个例子,你怎么可能能够解决这个问题。我假设,你有一个静态的长度数组(玩家最大数量)。

 播放器*玩家[MAX_PLAYERS] //数组的指针的播放器对象。
的for(int i = 0; I< MAX_PLAYERS ++ I)
    玩家[i] =新玩家(X,Y,Z); //填入一些数据阵列。

现在您擦除:

 如果(发现大于0){
    删除玩家[发现]; //销毁有问题的对象。
    的for(int i =发现; I< MAX_PLAYERS - 1 ++ I)
        玩家[I] =玩家第[i + 1]; //一个移动整个列表了。
    玩家[MAX_PLAYERS - 1] = NULL; //标记列表的新的终端。
}

这个小片段不会复制整个对象,而是他们在数组中向上移动(没有任何重建的对象)。

该阵列是在它的结束,当你遇到第一个NULL指针(和MAX_PLAYERS最新的),它占你的'空白'。或者,你可以省略动起来,只是销毁对象和指针设置为NULL。这样,你就会知道,有没有球员出现。

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.

thanks

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

解决方案

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.

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

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