c++ - 每个的附加条件

标签 c++ c++11

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

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/

相关文章:

c++ - 如何从实现与其他接口(interface)共享的接口(interface)的类继承

在 minGW-W64 g++ 中编译的 C++ 代码不能用 Ubuntu g++ 编译

c++ - 谷歌基准代码设置

c++ - 找出n个月前的确切日期和时间?

c++ - 使用可变参数模板和lambda函数进行二进制搜索

c++ - 没有相互引用和智能指针的观察者模式实现

c++ - 关于 C++ 仿函数/lambda 参数传递在 STL 算法中的混淆

c++ - SetScrollPos 阻塞

c++ - 具有堆栈分配的 C++ 类的 C 包装器

c++ - 通用回调函数的容器