在下一个循环之前访问基于 for 循环范围的下一个元素
Accessing next element in range based for loop before the next loop
如何在
下一个循环之前访问基于范围的 for 循环中的下一个元素?
我知道使用迭代方法可以执行类似 arr[++i]
的操作,但是如何在基于范围的 for 循环中实现相同的结果?
for (auto& cmd : arr) {
steps = nextElement; //How do I get this nextElement before the next loop?
}
我知道我可能不应该使用基于范围的 for 循环,但这是为这个项目提供的要求。
如果范围具有连续的存储(例如 std::vector
、std::array
、std::basic_string
或本机数组),那么你可以这样做:
for (auto& cmd : arr) {
steps = *(&cmd + 1);
}
否则,如果没有外部变量,则无法执行。
相关文章:
- 重新定位图像时如何前进到下一个内存块
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- CIN 仅在输入非数字值时跳过下一个 CIN
- 查找最小的下一个更大的元素
- 转到基于范围的 for 循环中的下一个迭代器
- 如何检查流中文件的下一个单词是否为 alpha?
- 如何打印下一个字母直到 Z 并继续到 A?
- C++循环链表删除,计数从下一个节点开始
- 如何将枯竭的健康转移到下一个循环中
- 需要帮助创建一个休眠条件的循环,当满足条件时,它会停止,直到下一个操作
- 更改了bool值,但在while语句的下一个循环中它又变了回来
- do while循环中的数组值没有传递给下一个
- 如何寻址循环中的下一个迭代器并在同一地址插入元素
- 在下一个循环之前访问基于 for 循环范围的下一个元素
- 虽然循环不输出子字符串的下一个实例
- 为什么当我按下一个字母时,会出现一个无限循环?我如何更改错误检查
- 如何在for循环中退出(转到父循环的下一个元素)for循环
- 跳到for循环Cuda中的下一个块
- 是否有一种方法可以在mysql_fetch_row()循环中发出下一个(如INSERT) mysql语句