基本上,我正在关注本教程:http ://cplussplussatplay.blogspot.com.cy/2012/11/text-adventure-games-c-part-1.html
到目前为止,我已经了解了所有内容,除了以下内容:
// Make words upper case
// Right here is where the functions from cctype are used
for(i = 0; i < words.size(); i++)
{
for(j = 0; j < words.at(i).size(); j++)
{
if(islower(words.at(i).at(j)))
{
words.at(i).at(j) = toupper(words.at(i).at(j));
}
}
}
此时,我们有一个充满字符的词向量。我不明白两个 for 循环的需要,也不明白这个 words.at(i).at(j))。它是二维向量还是什么?
提前致谢。
编辑:非常感谢大家的帮助!我现在明白了!这是第一次使用 Stack Overflow,到目前为止我很喜欢它!:)
还有一件事,另一个问题出现了!
string sub_str;
vector words;
char search = ' ';
// Clear out any blanks
// I work backwords through the vectors here as a cheat not to invalidate the iterator
for(i = words.size() - 1; i > 0; i--)
{
if(words.at(i) == "")
{
words.erase(words.begin() + i);
}
1.第二条评论是什么意思?
2.向量中怎么会有空格?根据创建者的第一个循环清除所有空白。这是之前的代码:
for(i = 0; i < Cmd.size(); i++)
{
if(Cmd.at(i) != search)
{
sub_str.insert(sub_str.end(), Cmd.at(i));
}
if(i == Cmd.size() - 1)
{
words.push_back(sub_str);
sub_str.clear();
}
if(Cmd.at(i) == search)
{
words.push_back(sub_str);
sub_str.clear();
}
}
再次感谢!