我正在尝试在 bool 向量上使用 any_of 函数。any_of 函数需要一个返回布尔值的一元谓词函数。但是,当输入到函数中的值已经是我想要的 bool 时,我无法弄清楚要使用什么。我会猜测一些函数名称,如“logical_true”或“istrue”或“if”,但这些似乎都不起作用。我在下面粘贴了一些代码以显示我正在尝试做什么。提前感谢您的任何想法。 - 克里斯
// Example use of any_of function.
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
vector<bool>testVec(2);
testVec[0] = true;
testVec[1] = false;
bool anyValid;
anyValid = std::find(testVec.begin(), testVec.end(), true) != testVec.end(); // Without C++0x
// anyValid = !std::all_of(testVec.begin(), testVec.end(), std::logical_not<bool>()); // Workaround uses logical_not
// anyValid = std::any_of(testVec.begin(), testVec.end(), std::logical_true<bool>()); // No such thing as logical_true
cout << "anyValid = " << …Run Code Online (Sandbox Code Playgroud)