首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++中.erase()的问题

c++中.erase()的问题
EN

Stack Overflow用户
提问于 2012-04-06 18:59:46
回答 3查看 383关注 0票数 1

我正在建立一个游戏库,我希望用户能够删除游戏。我正在尝试使用.erase()函数,但我在某个地方犯了一个错误。我研究我的代码好几天了,但我找不到一个答案。为什么?因为我只有15岁,没有其他程序员那样的编程头脑。这就是为什么我不称自己为程序员,而只称自己是一个无所适从的青少年。请帮帮我。(很抱歉我的拼写错误)

下面是我的代码:

代码语言:javascript
复制
int main()
{
    vector<string>::const_iterator myIterator;
    vector<string>::const_iterator iter;             
                                            vector<string> games;                                
    games.push_back("Crysis2");
    games.push_back("GodOfWar3");
    games.push_back("FIFA12");

    cout <<"Welcome to your Games Library.\n";
    cout <<"\nWarning!!! Don't type spaces, put ecerything together!!!\n";
    cout <<"\nThese are your games:\n";
    for (iter = games.begin(); iter != games.end(); ++iter)
    {
        cout <<*iter <<endl;
    }
    //the loop!
    string action;
    string newGame;

    cout <<"\n-Type 'exit' if you want to quit.\n-Type 'add' if you want to add a game.\n-Type 'delete' if you want to delete a game.\n-Type 'find' if you want to search a game.\n-Type 'game' if you don't know what game to play. ";

    while (action != "exit")
    {


        cout <<"\n\nWhat do you want to do: ";
        cin >> action;


        if (action == "add")
        {
            cout <<"\nType the name of the game you want to add: ";
            cin >> newGame;

            games.push_back(newGame);

            for (iter = games.begin(); iter != games.end(); ++iter)
            {
                cout <<*iter <<endl;
            }

            continue;
        }
        else if (action == "delete")
        {
            cout <<"Type the name of the game you want to delete: ";
            cin >> newGame;

            iter = find(games.begin(), games.end(), newGame);

            if(iter != games.end())
            {
                games.erase(newGame);
            }
            else
            {
                cout<<"\nGame not found.";
            }

            continue;
        }
        else if (action == "find")
        {
            cout <<"Which game you want to look for in your library: ";
            cin >> newGame;

            iter = find(games.begin(), games.end(), newGame);

            if (iter != games.end())
            {
                cout << "Game found.\n";
            }
            else
            {
                cout << "Game not found.\n";
            }

            continue;
        }
        else if (action == "game")
        {
            srand(static_cast<unsigned int>(time(0)));
            random_shuffle(games.begin(), games.end());
            cout << "\nWhy don't you play " <<games[0];

            continue;
        }
        else if (action == "quit")
        {
            cout <<"\nRemember to have fun while gaming!!\n";
            break;
        }
        else
        {
            cout <<"\nCommand not found";
        }
    }
    return 0;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-06 19:03:12

erase()接受迭代器(在本例中是find的结果,即iter),而不是字符串(如newGame)。

票数 2
EN

Stack Overflow用户

发布于 2012-04-06 19:05:25

您只是传递了要擦除的错误参数。代码

代码语言:javascript
复制
if(iter != games.end())
{
    games.erase(newGame);
}

应该是

代码语言:javascript
复制
if(iter != games.end())
{
    games.erase(iter);
}
票数 4
EN

Stack Overflow用户

发布于 2012-04-06 19:03:26

代码语言:javascript
复制
iterator erase ( iterator position );
iterator erase ( iterator first, iterator last );

尝试用games.erase(iter);替换games.erase(newGame);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10042552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档