正在创建我的收藏夹列表
Creating a list of my favorites
我正在创建的C++应用程序允许用户将他们喜欢的游戏添加或删除到列表中。
我想订购这份清单。
- 塞尔达传说
- Contra
- 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
。
等等。在标准库中有很多伟大的可能性。如果你错过了这些,同时又因为不知情地试图自己重塑它们而头疼,那将是一种耻辱
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 使用GCC的C makefile - 从子文件夹列表中生成源文件列表
- 如何使用 Win API 获取文件夹中的文件列表(带过滤器)
- QDIR入口列表可以使用文件夹快捷方式透明地工作
- 读取文件夹中的文件列表时获取分段折叠
- wxWidgets - 将所有文件夹写入列表框
- 如何在Windows中为当前用户获取库文件夹列表
- 按最顶端合并文件夹列表
- 如何获取给定文件夹中具有特定扩展名的文件列表
- 如何在Windows平台中获取远程Windows服务器的共享文件夹的文件列表
- 如何获得ftp服务器中所有文件和文件夹的列表(c++)
- 如何在c++中用一个目录中的所有文件夹填充一个列表框
- Linux dient:获取目录中所有文件夹的列表
- 获取用户计算机上文件名按文本行筛选的文件和文件夹列表
- 支持长双打的收藏夹/最佳A.X=B解算器
- 正在创建我的收藏夹列表
- 获取受UAC保护的文件夹列表
- 获取文件夹中文件的有序列表