std::none_of example from cppreference.com

std::none_of example from cppreference.com

本文关键字:from cppreference com example of none std      更新时间:2023-10-16

谁能帮我理解这个例子:

v 在这里是一个 std::vector。我认为这里的 std::bind 将其中的内容绑定到函数 std::none_of 因为它是条件。std::modulus取两个参数的mod。但是为什么他们的 _1 和 2 没有下划线。

if (std::none_of(v.cbegin(),
                 v.cend(),
                 std::bind(std::modulus<int>(), std::placeholders::_1, 2))) {
        std::cout << "None of them are oddn";
}
std::modulus<int>()是一个

接受两个参数的函数对象。

std::bind(std::modulus<int>(), std::placeholders::_1, 2)创建一个接受一个参数的函数对象。它需要一个参数,因为它只有一个占位符。std::modulus<int>()的第二个参数绑定到数字 2

然后,使用这个新的一元函数对象在v范围内调用none_of,在向量中的每个项目上调用它。所以基本上它是检查vx的每个元素的x % 2