正在创建我的收藏夹列表

Creating a list of my favorites

本文关键字:收藏夹 列表 我的 创建      更新时间:2023-10-16

我正在创建的C++应用程序允许用户将他们喜欢的游戏添加或删除到列表中。

我想订购这份清单。

  1. 塞尔达传说
  2. Contra
  3. GTA V

我知道我需要使用while/for循环,但我使用的是向量,我是一个新手,我不知道在代码中把while/ffor循环放在哪里。

到目前为止,我尝试过的是:

失败的尝试1-嵌套循环:

 for (gameInter = list.begin(); gameInter != list.end(); gameInter++) {
       for (int listNum = 1; listNum < list.size(); listNum++) {
            cout << listNum << ". " << *gameInter << endl;
        }
    }

尝试2失败-循环时出现错误:

case 1:
         while(listNum < list.size()) {
            cout << "Type the game title to add: n";
            cin.get();
            getline(cin, addGame);
                list.push_back(addGame);
                sort(list.begin(), list.end());
                cin.clear();
                cin.sync();
                cout << "nYour game was successfully added.n";
                listNum++;
         }
            break;

完整代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
    int userSelection = 0;
    int listNum = 1;
    string addGame, removeGame;
    vector<string> list;
    vector<string>::iterator gameInter;
    while (userSelection != 4) {
        cout << "Type 1 to add a game to your listn";
        cout << "Type 2 to remove a game from your listn";
        cout << "Type 3 to list all gamesn";
        cout << "Type 4 to quitn";
        cin >> userSelection;
        switch (userSelection) {
        case 1:
            cout << "Type the game title to add: n";
            cin.get();
            getline(cin, addGame);
                list.push_back(addGame);
                sort(list.begin(), list.end());
                cin.clear();
                cin.sync();
                cout << "nYour game was successfully added.n";
            break;
        case 2:
            cout << "Type the game title to remove: n";
            cin.get();
            getline(cin, removeGame);
            gameInter = find(list.begin(), list.end(), removeGame);
            if (*gameInter == removeGame) {
                cout << "Title " << removeGame << " foundn";
                list.erase(gameInter);
                cout << "Title " << removeGame << " has been removed!n";
            }
            else
            {
                cout << "Title " << removeGame << " cannot be found!n";
            }
            break;
        case 3:
            cout << "nYour Favorite Games Are: n";
                for (gameInter = list.begin(); gameInter != list.end(); gameInter++) {
                    cout << listNum << ". " << *gameInter << endl;
            }
            break;
        case 4:
            cout << "Thank You for your input! Goodbyen";
            //userSelection = 4;
            break;
        default:
            cout << "That is not a valid optionn";
            break;
        }
    }
    system("pause");
    return false;
} 

请考虑使用标准库提供的专用容器,让您的生活更轻松。它们的存在是为了避免你不得不费力地重新发明轮子,这样你就可以专注于你需要做的事情,而不是无聊的细节和周围的脚手架!不要误解我的意思:使用vector可以让你领先于许多人——不需要手动分配你自己的数组——但我们可以做得更好。

例如,创建一组具有等级和标题的游戏,这些游戏将自动按等级排序(如果出现平局,则按标题排序):

#include <iostream>
#include <set>
#include <string>
#include <tuple>  // pair
// Represent a game by its rank and title (typedef)
using game_type = std::pair<unsigned, std::string>;
// Create an ordered set of games
std::set<game_type> games;
// Populate it. You could do this from standard input.
games.emplace(3, "GTA V"); // constructs element in-place
games.emplace(1, "Legend Of Zelda");
games.emplace(2, "Contra");
// Verify that std::pair sorts by .first (and then .second)
for (auto const &it: games) { // tidy C++11 iteration syntax
    std::cout << it.first << ": " << it.second << std::endl;
}

输出就是你想要的:

1: Legend of Zelda
2: Contra
3: GTA V

想要从用户输入填充map吗?当然只需将排名和头衔获取到临时变量中,然后emplace(theRank, theTitle)

然后,您可以开始考虑如何根据排名进行擦除,并很快在std::set的文档或<algorithm>中找到答案。我看你已经看过后者了。太棒了!它在那里帮助我们,但有时会被忽视。或者人们使用它的功能,但不#include它,并依赖于其他地方的间接包容。。。

不管怎样。也许现在你希望/需要排名是唯一的,并且需要捕捉用户输入重复的情况?然后检查std::map

等等。在标准库中有很多伟大的可能性。如果你错过了这些,同时又因为不知情地试图自己重塑它们而头疼,那将是一种耻辱