zoukankan      html  css  js  c++  java
  • C++使用谓词函数

    源码示例:

    #include <iostream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    class IsEven
    {
    public:
      bool operator()(int x)
      {
        return x % 2 == 0;
      }
    };

    class LessThan
    {
    public:
      bool operator()(int a, int b)
      {
        return a < b;
      }
    };

    int main()
    {
      int arr[] {12,25,36,8,11,15,89,32,71};
      vector<int> vec{12,25,36,8,11,15,89,32,71};

      cout<<"Original form of both array and vector: ";
      for(int e:arr)
      {
        cout<<e<<" ";
      }

      //sort the array
      sort(begin(arr), end(arr),LessThan());

      //print the sorted array
      cout<<" Here is the aorted array: ";
      for(int e:arr)
      {
        cout<<e<<" ";
      }

      //Call remove_if for even values
      auto start_removed = remove_if(begin(vec), end(vec), IsEven());
      cout<<" Here is the vector after call to remove_if: ";
      for(int e:vec)
      {
        cout<<e<<" ";
      }

      //Erase the remove_if'd elements
      vec.erase(start_removed, end(vec));
      cout<<" Here is the vector after call to erase: ";
      for(int e:vec)
      {
        cout<<e<<" ";
      }
      cout<<endl;

      return 0;

    }

    结果展示:

  • 相关阅读:
    byvoid
    soa文章摘抄
    也谈设计模式,架构,框架和类库的区别
    GoF设计模式三作者15年后再谈模式
    陈梓涵:我们为什么要学习设计模式
    陈梓涵:关于编程的胡扯
    hung task机制
    iscsi target tgt架构
    iscsi target IET架构
    ISCSI工作流程target和initiator
  • 原文地址:https://www.cnblogs.com/ruigelwang/p/12635341.html
Copyright © 2011-2022 走看看