Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在下一个循环之前访问基于范围的 for 循环中的下一个元素?
我知道使用迭代方法可以执行类似的操作arr[++i],但是如何在基于范围的 for 循环中获得相同的结果?
arr[++i]
for (auto& cmd : arr) { steps = nextElement; //How do I get this nextElement before the next loop? }
我知道我可能不应该使用基于范围的 for 循环,但这是为这个项目提供的要求。
如果范围具有连续存储(例如std::vector、或本机数组),那么您可以这样做std::array:std::basic_string
std::vector
std::array
std::basic_string
for (auto& cmd : arr) { steps = *(&cmd + 1); }
否则,你不能,没有外部变量。