功能描述:

    • 实现逻辑运算

    函数原型:

    • template bool logical_and //逻辑与
    • template bool logical_or //逻辑或
    • template bool logical_not //逻辑非

    代码演示:

    1. #include<iostream>
    2. using namespace std;
    3. #include<vector>
    4. #include<algorithm>
    5. #include<functional>//内建函数对象的头文件
    6. //内建函数对象 逻辑仿函数
    7. //logical_not(逻辑非)
    8. void test01() {
    9. vector<bool>a;
    10. a.push_back(true);
    11. a.push_back(false);
    12. a.push_back(true);
    13. a.push_back(false);
    14. for (vector<bool>::iterator it = a.begin(); it != a.end(); it++) {
    15. cout << *it << " ";
    16. }
    17. cout << endl;
    18. //利用逻辑非,将容器取反后转到另一个容器
    19. vector<bool>b;
    20. b.resize(a.size());
    21. transform(a.begin(), a.end(),b.begin(),logical_not<bool>());
    22. for (vector<bool>::iterator it = b.begin(); it != b.end(); it++) {
    23. cout << *it << " ";
    24. }
    25. cout << endl;
    26. }
    27. int main() {
    28. test01();
    29. return 0;
    30. }

    运行结果:
    image.png