zoukankan      html  css  js  c++  java
  • C++ lamda、function、bind使用

    参考资料:

    http://blog.csdn.net/augusdi/article/details/11771699

    • lambda 表达式的简单语法如下:[capture] (parameters) -> return value { body }

    其中[capture]可以选择如下的不同形式:

    clipboard

          使用示例:

    clipboard[1]

    clipboard[2]

    • function

            std::function对象是对C++中现有的可调用实体的一种类型安全的包裹,std::function 使用

    clipboard[3]

    • bind

            std::bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体,这种机制在回调函数的使用过程中也颇为有用。 C++0x中,提供了std::bind,它绑定的参数的个数不受限制,绑定的具体哪些参数也不受限制.

    clipboard[4]

    注:

        对于不事先绑定的参数,需要传std::placeholders进去,从_1开始,依次递增。placeholder是pass-by-reference的占位符。

    • 综合示例:
      1、EmailProcessor.h
      #pragma once
      
      #include <functional>
      #include <string>
      using namespace std;
      
      class EmailProcessor
      {
          private:
          function<void (const string&)> _handle_func;
      
          public:
          void receiveMessage(const string& str)
          {
              if(_handle_func)
              {
              _handle_func(str);
              }
          }
      
          void setHandlerFunc(function<void (const string&)> func)
          {
              _handle_func=func;
          }
      };

      2、MessageStored.h
      #pragma once
      #include <string>
      #include <vector>
      #include <iostream>
      using namespace std;
      
      class MessageStored
      {
          private:
          vector<string> _store;
      
          bool find(const string& str,const string& key)
          {
              return [&](){
                  size_t pos=str.find(key);
                  return pos!=string::npos && str.substr(pos)==key;
              }();
          }
      
          public:
          bool checkMessage(const string& str)
          {
              for(auto iter=_store.begin(),end=_store.end();
                  iter!=end;iter++)
              {
              if(find(str,*iter))
              {
                  cout<<"Sending Msg "<<str<<endl;
                  return true;
              }
              }
              cout<<"no match email"<<endl;
              return false;
          }
      
          void addMsgStore(const string str)
          {
              _store.push_back(str);
          }
      };

      3、main.cpp
      #include "EmailProcessor.h"
      #include "MessageStored.h"
      #include <iostream>
      using namespace std;
      
      
      int main()
      {
          MessageStored stored;
          string mails[]={"@163.com","@qq.com","@gmail.com"};
          for(string str:mails)
          {
          stored.addMsgStore(str);
          }
      
          EmailProcessor processor;
          auto func=std::bind(&MessageStored::checkMessage,stored,placeholders::_1);
          processor.setHandlerFunc(func);
          processor.receiveMessage("xxx@gmail.com");
      }

    注:

          &MessageStored::checkMessage是获取类成员函数的地址

  • 相关阅读:
    Winsock 2 入门指南
    Winsock 2 入门指南
    [手游新项目历程]-40-linux环境实现C/C++程序崩溃退出时打印栈信息
    1月下旬解题
    poj1226,poj3080
    poj3666
    poj3067
    poj12月其他题解(未完)
    poj1823,3667
    poj2352
  • 原文地址:https://www.cnblogs.com/luosongchao/p/4082419.html
Copyright © 2011-2022 走看看