我想知道:是否有可能为每个条件添加额外的条件? 我在想类似的事情:
int i=0;
for(auto &it : list; i++)
if(it.ID == 25)
return i;
或
for(auto &it : list, int i=0; i++)
if(it.ID == 25)
return i;
最佳答案
你可以使用std::find_if
:
const auto position = std::find_if(list.cbegin(), list.cend(), []((decltype(*list.cbegin()) value)
{
return value.ID == 25;
});
return position - list.cbegin();
(已更新,现在独立于容器 value_type)
关于c++ - 每个的附加条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18778972/