【发布时间】:2013-12-24 17:27:17
【问题描述】:
我正在尝试在布尔向量上使用 any_of 函数。 any_of 函数需要一个返回布尔值的一元谓词函数。但是,当输入到函数中的值已经是我想要的布尔值时,我不知道该使用什么。我猜想一些函数名称,如“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 = " << anyValid <<endl;
return 0;
}
【问题讨论】:
标签: c++ stl boolean predicate unary-operator