zoukankan      html  css  js  c++  java
  • 仿函数

    ceres里面用到仿函数,故单独测试一下用法

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    class LessThenLenFunctor{
    public:
        bool operator()(const string& str){
            return str.length() < len;
        }
    
        //构造函数
        LessThenLenFunctor(int l):len(l){};
    
    private:
        int len;
    };
    
    int main()
    {
        vector<string> sVec{"aegew", "ssftat", "12"};
        int cnt = count_if(sVec.begin(), sVec.end(),LessThenLenFunctor(6));
        cout << cnt << endl;
    }

    可以让LessThenLenFunctor类作为一个函数被使用,其中类的成员变量作为这个函数的参数,具有较大的灵活性。

    参考:https://blog.csdn.net/codedoctor/article/details/79654690

    另外,也可以通过lamda(匿名函数)来实现相同的功能

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    class LessThenLenFunctor{
    public:
        bool operator()(const string& str){
            return str.length() < len;
        }
    
        //构造函数
        LessThenLenFunctor(int l):len(l){};
    
    private:
        int len;
    };
    
    int main()
    {
        vector<string> sVec{"aegew", "ssftat", "12"};
        int x = 6;
       // int cnt = count_if(sVec.begin(), sVec.end(),LessThenLenFunctor(6));
       int cnt = count_if(sVec.begin(), sVec.end(), [x](string str){return str.length() < x;});
        cout << cnt << endl;
    }

     关于lamda的使用,https://blog.csdn.net/bajianxiaofendui/article/details/86583612,这里讲的比较清楚,自己也可以验证一下。

  • 相关阅读:
    js 秒的倒计时,将秒转换为时分秒显示
    mysql 中 int 等类型如何选择
    js 经常用于条件判断 大于等于0 的正整数
    egg.js 相关
    nodejs 开发时,学用的热更新工具 nodemon
    pm2 工具来管理 node 服务端
    centos 宝塔面版 运行 thinkjs
    图解ByteBuffer
    gc HeapTaskDaemon守护线程
    Android Bitmap变迁与原理解析(4.x-8.x)
  • 原文地址:https://www.cnblogs.com/havain/p/15506892.html
Copyright © 2011-2022 走看看