c++ - 在 std::all_of 中调用一个函数

标签 c++ c++11 stl

<分区>

我知道我能做到:

 vector<int> insidetest;

 if(std::all_of(insidetest.begin(),insidetest.end(),[](int i){return i>100;}))
    {
       std::cout << "All greater" << std::endl;
    }

但我想调用另一个函数(可能比 >1000 更复杂)。我如何调用 std::all_of 中的另一个函数,例如:

   bool fun(const vector<int> *s)
   {
   return true;
   }

最佳答案

如果 fun 有这样的签名 - 没有办法。 它有趣有签名bool(int)然后简单地写

if(std::all_of(insidetest.begin(),insidetest.end(),fun))

如果你想要函数中的其他参数 - 你可以使用 std::bind 例如签名 bool(int, int, int)

bool fun(int value, int min, int max)
{
   return value > min && value < max;
}

if(std::all_of(insidetest.begin(),insidetest.end(),
               std::bind(fun, std::placeholders::_1, 1, 5)))

关于c++ - 在 std::all_of 中调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16140380/

相关文章:

c++ - 将左值绑定(bind)到右值引用——g++ 错误?

c++ - 在 C++ 中使用 sprintf() 和 sscanf() 时出现意外行为?

c++ - 如果每个线程都有自己的只读数组拷贝,线程会更快吗

c++ - 如何使 map::find 操作不区分大小写?

C++ STL 错误的键类型

c++ - 程序特定的 OpenGL 运行时错误 : Multiple Input Buffers For Skinned Animation

c++ - Qt:检测鼠标是否位于某个小部件上,即使该小部件没有焦点

c++ - 如何在类中传递函数作为参数?

c++ - std::pair 提示类型不完整

c++ - 在我的 C++ 结构中初始化一个空的 unordered_map