c++为下一次迭代保留索引
C++ preserve indexes for next iteration?
我有一个循环,我想为下一次迭代保留值。我决定使用std::vector
(虽然我不知道它是否是最好的方式),但当我检查if(bottomMap[i] == true)
时,它不编译。
vector<bool> bottomMap;
for (int i = 0; i < str.size() ; i++){
if (str[i] != ' ') {
cout << "XXXX";
} else {
if (bottomMap[i] == true)
cout << "YYYY";
else
cout << "X";
}
}
vector <bool> bottomMap(topMap);
}
bottomMap必须在循环开始前存在
for (int i = 0; i < str.size() ; i++){
if (str[i] != ' ') {
cout << "XXXX";
} else {
if (bottomMap[i] == true) //< ERROR: bootomMap here is not declred.
cout << "YYYY";
else
cout << "X";
}
}
vector <bool> bottomMap(topMap); //< Declared here, but destroyed to the very next '}'
}
Try this
vector <bool> bottomMap(topMap); //< MOVED HERE
for (int i = 0; i < str.size() ; i++){
if (str[i] != ' ') {
cout << "XXXX";
} else {
if (bottomMap[i] == true)
cout << "YYYY";
else
cout << "X";
}
}
///< AND NOMORE HERE
}
建议:保持左括号和右括号对齐:这将花费更多的行,这将很容易找到东西的开始和结束
这是你的代码,重新排列:
for (int i = 0; i < str.size() ; i++)
{
if (str[i] != ' ')
{
cout << "XXXX";
}
else
{
if (bottomMap[i] == true) //< ERROR: bottomMap here is not declared.
cout << "YYYY";
else
cout << "X";
}
}
vector <bool> bottomMap(topMap); //< Declared here, but too late!
} //< ERROR: TOO CLOSED BRACES
因为在你使用bottommap
的地方,它不知道它的定义。你必须在使用它之前定义它
相关文章:
- 在一次迭代中从 txt 文件中读取多行
- 高级选择排序 - 在一次迭代中搜索两个元素
- foor 循环的最后一次迭代中的指针更改
- 循环中本地对象的析构函数是否保证在下一次迭代之前被调用?
- ifstream不会在下一次迭代中打开文件
- C++如何使用lambda表达式来捕获上一次迭代的值
- 如何强制循环执行另一次迭代到零并关闭循环
- C 一次迭代所有仿制药
- 具有两个变量的"for"循环,一个变量每次迭代递增,每隔一次迭代递增
- while(getline(myReadFile, temp, ':')) 执行一次迭代太多导致向量越界
- 如何在C++中的while循环中存储上一次迭代
- 如何将while循环的每一次迭代都分配给线程
- 为什么用于解析我读取的行的内部循环每隔一次迭代就会通过外部读取循环跳过
- 有没有办法在一次迭代中进行任意数量的正则表达式替换
- z 阶曲线中的下一次迭代
- OpenCV神经网络一次训练一次迭代
- While循环在一次迭代后终止
- c++为下一次迭代保留索引
- 在while循环的下一次迭代中重写Const char *
- 通过一次迭代删除字符串中的所有逗号、句点和小写字母