c++ - 在 C++11 "foreach"语句中是否有任何(方便的)方法来检索当前迭代#?

标签 c++ c++11

我想知道是否有可能以某种方式从 C++11 foreach 语句中提取当前迭代次数。

在这样的代码中:

for(auto &i: vect)
    if(i == 0) zero_value_index = /* here I want my index */;

我找不到其他方法,只能使用老式的 forint i 轻松获取我的索引。

想法?

最佳答案

我不知道,你可以计算迭代次数:

int i = 0;
for (auto& el : container) {
    if (el == 0) zero_value_index = i;
    ++i;
}

关于c++ - 在 C++11 "foreach"语句中是否有任何(方便的)方法来检索当前迭代#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23436939/

相关文章:

c++ - 用指针 C++ 实现双向链表

c++ - “does not name a type” 错误 C++

c++ - "Inheriting"具有可变模板函数的类

c++ - 确保派生类实现静态方法

c++ - 为什么我不能在 vector 中放置 ifstreams?

C++ 观察者模式 : adding another dimension

c++ - 使用VS Code在C++应用程序中查找内存泄漏

c++ - 检查串口是否有数据传入linux(cbInQue for linux)

c++ - 存储数据结构c++

C++11 异步 + 然后