我正在创建的C ++应用程序允许用户将他们喜欢的游戏添加或删除到列表中。
我希望订购这份清单。
我知道我需要使用while / for循环,但是我使用的是vector(s),我是新手,我不知道在我的代码中将while / for循环放在哪里。
到目前为止我尝试的是:
尝试失败1 - 嵌套for循环:
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 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;
}
答案 0 :(得分:0)
请考虑使用标准库提供的专用容器,让您的生活更轻松。它们的存在是为了避免您不得不费力地重新发明轮子,这样您就可以专注于您需要做的事情,而不是无聊的细节和周围的脚手架!不要误会我的意思:使用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
。
等等。标准库中有很多可能性。如果你错过了那些同时也会在不知不觉中试图重新发明它们而感到头痛,那将是一种耻辱。 ; - )