6

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 = " << anyValid <<endl;

    return 0;
}
4

4 に答える 4