我在编写练习代码时遇到了这个奇怪的问题。
首先,当我选择第一个选项时,输入一个错误的条目。它应该转到else
我的代码的一个分支,但它却卡在那里。我真的不知道为什么。当我输入一个中间有空格的“游戏标题”时也会发生这种情况。
其次,我在删除分支注释掉的那一行:
iter = gameTitles.erase(iter);
......根本不起作用。我正在尝试做的是通过输入来删除一个条目,然后将其与一个条目进行比较,以便它知道要删除什么。这就是为什么我也在使用迭代器。
// Exercise 1
/*
Write a program using vectors and iterators that allows a user to maintain a list of
his or her favorite games. The program should allow the user to list all game titles,
add a game title, and remove a game title.
*/
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
using namespace std;
int main(){
bool bLoop = true;
int nChoice;
char cChoice;
string sInput;
vector<string>::const_iterator iter;
vector<string> gameTitles;
while(bLoop){
// -Head
cout << "///////////////////////////////////\n// My Favorite Games\n\n";
cout << "1. Add title\n2. Delete title\n3. Clear list\n\n";
// -List
if(!gameTitles.empty()){
for(iter = gameTitles.begin(); iter!=gameTitles.end(); ++iter){
cout << "-" << *iter << endl;
}
}
cout << "\n:: ";
cin >> nChoice;
// 1. Add
if(nChoice == 1){
cout << "\nGame Title: ";
cin >> sInput;
gameTitles.push_back(sInput);
}
// 2. Delete
else if(nChoice == 2) {
cout << "Delete Title: ";
cin >> sInput;
for(iter = gameTitles.begin(); iter!=gameTitles.end(); ++iter){
if(*iter == sInput){
cout << "erased";
//iter = gameTitles.erase(iter);
}
}
}
// 3. Clear
else if(nChoice == 3){
cout << "Are you sure? (y/n) ";
cin >> cChoice;
if(cChoice == 'y'){
gameTitles.clear();
}
} else {
cout << "\nInvalid Choice, Please try again.\n";
}
// -Clean
system("PAUSE");
system("cls");
}
}
编辑:解决了第一个问题。使用普通迭代器而不是常量迭代器
EDIT2:解决了第二个问题,这是我更正的代码:
// Exercise 1
/*
Write a program using vectors and iterators that allows a user to maintain a list of
his or her favorite games. The program should allow the user to list all game titles,
add a game title, and remove a game title.
*/
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
using namespace std;
int main(){
bool bLoop = true;
int nChoice;
char cChoice;
string sInput;
vector<string>::iterator iter;
vector<string> gameTitles;
while(bLoop){
// -Head
cout << "///////////////////////////////////\n// My Favorite Games\n\n";
cout << "1. Add title\n2. Delete title\n3. Clear list\n\n";
// -List
if(!gameTitles.empty()){
for(iter = gameTitles.begin(); iter!=gameTitles.end(); ++iter){
cout << "-" << *iter << endl;
}
}
cout << "\n:: ";
cin >> nChoice;
if(cin.fail()){
cin.clear();
cin.ignore();
}
// 1. Add
if(nChoice == 1){
cout << "\nGame Title: ";
cin >> sInput;
gameTitles.push_back(sInput);
}
// 2. Delete
else if(nChoice == 2) {
cout << "Delete Title: ";
cin >> sInput;
for(iter = gameTitles.begin(); iter!=gameTitles.end(); ){
if(*iter == sInput){
cout << "erased";
iter = gameTitles.erase(iter);
} else {
++iter;
}
}
}
// 3. Clear
else if(nChoice == 3){
cout << "Are you sure? (y/n) ";
cin >> cChoice;
if(cChoice == 'y'){
gameTitles.clear();
}
} else {
cout << "\nInvalid Choice, Please try again.\n";
}
// -Clean
system("PAUSE");
system("cls");
}
}