如何在下一个循环之前访问基于 for 循环范围内的下一个元素?
我知道使用迭代方法您可以执行类似的操作arr[++i],但是如何在基于范围的 for 循环中实现相同的结果?
for (auto& cmd : arr) {
steps = nextElement; //How do I get this nextElement before the next loop?
}
Run Code Online (Sandbox Code Playgroud)
我知道我可能不应该使用基于范围的 for 循环,但这是为该项目提供的要求。
如果范围有连续的存储(例如std::vector,std::array,std::basic_string或天然阵列),则可以做到这一点:
for (auto& cmd : arr) {
steps = *(&cmd + 1);
}
Run Code Online (Sandbox Code Playgroud)
否则,如果没有外部变量,你就不能。
| 归档时间: |
|
| 查看次数: |
5491 次 |
| 最近记录: |