每个附加条件

Dez*_*teR 2 c++ c++11

我想知道:有没有可能为每个添加额外的条件?我正在考虑类似的事情:

int i=0;
for(auto &it : list; i++)
    if(it.ID == 25)
        return i;
Run Code Online (Sandbox Code Playgroud)

或者

for(auto &it : list, int i=0; i++)
    if(it.ID == 25)
        return i;
Run Code Online (Sandbox Code Playgroud)

ste*_*fan 5

您可以使用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();
Run Code Online (Sandbox Code Playgroud)

(更新,现在独立于容器 value_type)