我在为练习编写代码时遇到了这个奇怪的问题。
首先,当我选择第一个选项时,输入错误的条目。它应该转到我的代码的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");
}
}
答案 0 :(得分:2)
实际上有两个问题:
if (std::cin >> nChoice) { /* actual processing */ }
之类的内容验证输入是否成功。请注意,输入失败时nChoice
的值不会更改。如果输入失败,则需要进行一些错误恢复:流已进入失败状态(即,在错误标志中设置了std::ios_base::failbit
)并且在得到{{之前不会拒绝进行任何进一步的输入1}}编这仍然会在输入中留下令人不快的字符,您可能想要clear()
。为什么使用ignore()
的循环行为不正常?当您实际执行erase()
一个值时,您不希望在循环结束时再次递增迭代器。如果这样做,它可能会很好地将迭代器移到最后,从而导致未定义的行为。也就是说,循环看起来应该是这样的:
erase()
当然,相同逻辑的简短版本是:
for(iter = gameTitles.begin(); iter!=gameTitles.end(); ) {
if (*iter == sInput) {
cout << "erased";
iter = gameTitles.erase(iter);
}
else {
++iter;
}
}
答案 1 :(得分:0)
我认为你弄错了,从容器的iterator
方法返回的erase
永远不会指向从该容器中删除的值,假设这样:
std::vector<std::string> v;
// push some items to v
v.push_back( "1" );
v.push_back( "2" );
v.push_back( "3" );
auto i = v.erase( v.end() - 1 );
// Now i point to end of v and you can't derefrence it
相反,它指向您之前包含您的值的vector
中的位置:
auto i = v.erase( v.begin() );
assert( *i == "2" );