zoukankan      html  css  js  c++  java
  • C++之not1和not2

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

    not1:

     1 // not1 example
     2 #include <iostream>     // std::cout
     3 #include <functional>   // std::not1
     4 #include <algorithm>    // std::count_if
     5 
     6 struct IsOdd {
     7   bool operator() (const int& x) const {return x%2==1;}
     8   typedef int argument_type;
     9 };
    10 
    11 int main () {
    12   int values[] = {1,2,3,4,5};
    13   int cx = std::count_if (values, values+5, std::not1(IsOdd()));
    14   std::cout << "There are " << cx << " elements with even values.
    ";
    15   return 0;
    16 }

    not2:

     1 // not2 example
     2 #include <iostream>     // std::cout
     3 #include <functional>   // std::not2, std::equal_to
     4 #include <algorithm>    // std::mismatch
     5 #include <utility>      // std::pair
     6 
     7 int main () {
     8   int foo[] = {10,20,30,40,50};
     9   int bar[] = {0,15,30,45,60};
    10   std::pair<int*,int*> firstmatch,firstmismatch;
    11   firstmismatch = std::mismatch (foo, foo+5, bar, std::equal_to<int>());
    12   firstmatch = std::mismatch (foo, foo+5, bar, std::not2(std::equal_to<int>()));
    13   std::cout << "First mismatch in bar is " << *firstmismatch.second << '
    ';
    14   std::cout << "First match in bar is " << *firstmatch.second << '
    ';
    15   return 0;
    16 }

    本文来自博客园,作者:Mr-xxx,转载请注明原文链接:https://www.cnblogs.com/MrLiuZF/p/14078499.html

  • 相关阅读:
    Java:多线程<一>
    Java:Exception
    Java: 内部类
    Ubuntu安装jdk
    ubuntu搜狗拼音安装
    录音-树莓派USB摄像头话筒
    leetcode 最小栈
    leetcode 编辑距离 动态规划
    leetcode 最小覆盖字串
    leetcode 最长上升子序列 动态规划
  • 原文地址:https://www.cnblogs.com/MrLiuZF/p/14078499.html
Copyright © 2011-2022 走看看