将C ++ Vector作为数字访问

时间:2016-08-16 20:38:48

标签: c++ vector

所以基本上我想要一个游戏列表,并给用户删除它的选项。它看起来像这样:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
//setup
vector<string> games;
games.push_back("Team Fortress 2");
games.push_back("Skyrim");
games.push_back("Portal");
vector<string>::iterator myIterator

//main
int delItem;
cin >> delItem; // Consumes a number to represent the game to be deleted

while (iter != games.end()) // Displays all the games
{
    cout << j << ") " << *iter << endl;
    iter++;
    j++;
}

while ((delItem <= games.begin()) || (delItem > games.end())) // can only be 1, 2, or 3
{
    cout << "\nThat input is incorrect. Please try again: ";
    getline (cin, delItem);
}
myIterator = (games.begin() + (delItem - 1);
games.erase(myIterator); // Deletes the item
cout << "\nThe game has been deleted.";

return 0;
}

所以当我显示列表时,它看起来像这样:
1)Team Fortress 2
2)天际
3)门户

用户可以键入游戏之前的数字,并选择将其删除。我想要做的是防止用户输入高于或低于这三个数字的数字。我在第二个while循环中尝试这个,但看起来game.begin()不是数字。我知道这可能是一个我几乎不会错过的愚蠢错误,但任何帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

你是对的games.begin()不是数字。它是iterator。但是,您可以使用games.size()来确定games向量中的元素数量。您可以将其用于用户可以键入的最大数量。据我所知,最小数量将始终相同。我把它作为练习让读者确定它是什么。

答案 1 :(得分:0)

您的代码存在一些问题。

在您打印出可供用户选择的可用选项之前,您正在阅读用户的输入。你需要扭转这些操作。

您正在使用尚未声明的变量。

当用户输入无效号码时,您拨打std::getline()来获取新号码,但std::getline()输出到std::string,而不是int。< / p>

尝试更像这样的东西:

#include <iostream>
#include <string>
#include <vector>
#include <limits>

using namespace std;

int main()
{
    //setup
    vector<string> games;
    games.push_back("Team Fortress 2");
    games.push_back("Skyrim");
    games.push_back("Portal");
    vector<string>::iterator myIterator;

    //main

    int j = 1;
    for (myIterator = games.begin(); myIterator != games.end(); ++myIterator) // Displays all the games
    {
        cout << j << ") " << *myIterator << endl;
        ++j;
    }

    cout << "\nPick an item to delete: ";

    int delItem;
    do
    {
        if (cin >> delItem) // Consumes a number to represent the game to be deleted
        {
            if ((delItem >= 1) && (delItem <= games.size())) // can only be 1, 2, or 3
                break;
        }

        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin.clear();

        cout << "\nThat input is incorrect. Please try again: ";
    }
    while (true);

    /*
    alternatively:

    int delItem;
    do
    {
        string line;
        if (!getline(cin, line)) {
            // error!
            return 0;
        }

        istringstream iss(line);
        if (iss >> delItem) // Consumes a number to represent the game to be deleted
        {
            if ((delItem >= 1) && (delItem <= games.size())) // can only be 1, 2, or 3
                break;
        }

        cout << "\nThat input is incorrect. Please try again: ";
    }
    while (true);
    */

    myIterator = games.begin() + (delItem - 1);
    games.erase(myIterator); // Deletes the item
    cout << "\nThe game has been deleted.";

    return 0;
}