gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:00 26 4
gpt4 key购买 nike

我想知道是否有可能以某种方式从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com