6

如何在下一个循环之前访问基于范围的 for 循环中的下一个元素?

我知道使用迭代方法可以执行类似的操作arr[++i],但是如何在基于范围的 for 循环中获得相同的结果?

for (auto& cmd : arr) {
   steps = nextElement; //How do I get this nextElement before the next loop?
}

我知道我可能不应该使用基于范围的 for 循环,但这是为这个项目提供的要求。

4

1 回答 1

9

如果范围具有连续存储(例如std::vector、或本机数组),那么您可以这样做std::arraystd::basic_string

for (auto& cmd : arr) {
    steps = *(&cmd + 1);
}

否则,你不能,没有外部变量。

于 2013-11-14T00:49:12.507 回答