我想知道是否有可能以某种方式从 C++11 foreach 语句中提取当前迭代次数。
在这样的代码中:
for(auto &i: vect)
if(i == 0) zero_value_index = /* here I want my index */;
我找不到其他方法,只能使用老式的 for
和 int 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/