美文网首页
C++ STL 否定谓词 not1() not2()

C++ STL 否定谓词 not1() not2()

作者: book_02 | 来源:发表于2019-06-01 19:56 被阅读0次

作用

not1是构造一个与谓词结果相反的一元函数对象。
not2是构造一个与谓词结果相反的二元函数对象。

头文件

#include <functional>

not1 例子

#include <iostream>
#include <vector>
#include <functional>

int main(int argc, char **argv) 
{  
    std::vector<int> nums = {5, 3, 4, 9, 1, 7, 6, 2, 8};
    
    std::function<bool(int)> less_than_5 = [](int x){ return x <= 5; };
    
    // count numbers of integer that not less and equal than 5
    std::cout << std::count_if(nums.begin(), nums.end(), std::not1(less_than_5)) << "\n";
    
    return 0;
}
4

not2 例子

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main(int argc, char **argv) 
{  
    std::vector<int> nums = {5, 3, 4, 9, 1, 7, 6, 2, 8};
    
    std::function<bool(int, int)> ascendingOrder = [](int a, int b) { return a<b; };
    
    // sort the nums in descending order: not ascending order
    std::sort(nums.begin(), nums.end(), std::not2(ascendingOrder));
    
    for(int i:nums) {
        std::cout << i << "\t";
    }
    
    return 0;
}
9   8   7   6   5   4   3   2   1   

参考资料

std::not1
https://en.cppreference.com/w/cpp/utility/functional/not1

std::not2
https://en.cppreference.com/w/cpp/utility/functional/not2

相关文章

  • C++ STL 否定谓词 not1() not2()

    作用 not1是构造一个与谓词结果相反的一元函数对象。not2是构造一个与谓词结果相反的二元函数对象。 头文件 #...

  • 读书笔记17.06.03

    C++ STL:Listlist是C++标准模版库(STL,Standard Template Library)中...

  • [C++] STL 容器

    参考:[C++] STL 容器 (一) - 基本介紹[C++] STL 容器 (二) - Iterator 部分示例:

  • c++进阶-STL函数对象和谓词

    Map容器的介绍和使用 添加数据,一共四种方式,第一种和第四种最常用。 查找数据find 添加数据的四种方式的区别...

  • C++ STL 学习笔记

    C++ STL 学习笔记

  • STL之参考文献

    C++标准库是离不开模板的,STL占了C++标准库80%以上。 学习STL(c++ 98)的主要参考: gcc 3...

  • 任务列表

    C++ 《C++ primer》、《STL源码解析》、《effective C++》、《深度搜索c++对象模型》 ...

  • STL初认识

    一 C++ 与STL 历史 STL全称standard template library,由Alexander S...

  • STL算法之谓词

    概述 返回值类型为bool的普通函数或函数对象都叫谓词。 分类 一元谓词:一个参数 一般用于查找策略 二元谓词:两...

  • C++入门系列博客五 C++ STL

    C++ 标准模板库(STL) 作者:AceTan,转载请标明出处! 0x00 何为STL## STL(Standa...

网友评论

      本文标题:C++ STL 否定谓词 not1() not2()

      本文链接:https://www.haomeiwen.com/subject/hjoatctx.html