创建我的收藏夹列表

问题描述:

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

我想要订购清单。

  1. 传说塞尔达
  2. 魂斗罗
  3. GTA V

我知道我将需要使用一段时间/ for循环,但是我使用的矢量(S)我是新手,并且我不知道在我的代码中放置while/for循环的位置。

我迄今为止尝试是:

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

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

尝试失败2 - while循环:

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 list\n"; 
     cout << "Type 2 to remove a game from your list\n"; 
     cout << "Type 3 to list all games\n"; 
     cout << "Type 4 to quit\n"; 

     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 << " found\n"; 
       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! Goodbye\n"; 
      //userSelection = 4; 
      break; 
     default: 
      cout << "That is not a valid option\n"; 
      break; 
     } 
    } 
    system("pause"); 
    return false; 
} 
+3

1)这就是为什么一个人不应该在全局范围内使用'namespace std':'vector list;'。即不要将您的变量命名为STL类型。 2)你遇到什么错误? –

+0

向下选民:请评论! –

+0

根据评论#1和“失败的尝试” - _什么失败?你不能只说'它不工作',并期望一个没有描述问题的解决方案。这种方法的受欢迎程度永远不会令我惊叹:人们如何期待它的工作?无论如何,当stdlib为像这样的情况提供奇妙的功能时,例如'std :: set >',您应该可以省去手动重新发明轮子的麻烦,它会根据'pair :: first'中的等级自动排序(如果存在任何关系,则在'pair :: second'中输入名称)。 –

请考虑使用标准库提供的专用容器,以使您的生活更轻松。它们的存在是为了避免你不得不辛苦地重新发明轮子,这样你就可以专注于你需要做的事情,而不是无聊的细节和脚手架!不要误会我的意思:使用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

依此类推。标准库中有很多很棒的可能性。如果你错过了这些东西,同时也会在不知不觉中试图重塑他们,那将是一件耻辱。 ;-)