首页 > 编程知识 正文

struct函数使用,std::cout<<"i="<<i<<std::endl

时间:2023-05-06 10:55:44 阅读:262948 作者:4295



//todo
/*


[root@Slave02 thread]# g++ bind.cpp  -o bind -g -Wall -std=gnu++11 -lpthread;./bind
使用函数指针,调用全局函数 f,11
class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,0
class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,0


class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,1
class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,1
class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,1
class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,2
class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,2
class void Test::print(const std::string& str,int32_t i) 对比测试 std::mem_fn 和 std::bind,并调用类重载函数,2


class void Test::print() no parameter,调用无参数函数
class void Test::print() no parameter,调用无参数函数
class void Test::print() no parameter,调用无参数函数


class void Test::print() no parameter,调用无参数函数
class void Test::print() no parameter,调用无参数函数
class void Test::print() no parameter,调用无参数函数


*/


#ifndef _BIND_INCLUDE_H
#define _BIND_INCLUDE_H


#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#endif


using namespace std;
using namespace placeholders;


class Test
{
  public:
    void print(const std::string& str,int32_t i)
    {
    std::stringstream ss;
        ss << "class void Test::print(const std::string& str,int32_t i) " << str << "," << i << "n";
        std::cout << ss.str();
    }
    
    void print()
    {
        std::stringstream ss;
        ss << "class void Test::print() no parameter,调用无参数函数" << "n";
        std::cout << ss.str();
    }
};


void f(int32_t i)
{
std::cout<<"使用函数指针,调用全局函数 f,"<<i<<endl;
return ;
}


int32_t main(int32_t argc, char *argv[])
{    
    //使用传统函数指针调用
    typedef void (*G_FP)(int32_t) ;
    G_FP ff = f;
    ff(11);
    
    Test t1,t2,t3;
    std::string s = "对比测试 std::mem_fn 和 std::bind,并调用类重载函数";
    int32_t i = 0;
    //使用传统的类成员函数指针调用
    typedef void (Test::*C_MFP)(const std::string &, int32_t) ;
    C_MFP mfp_print = &Test::print;
    // *fff 是函数指针的语法表示
    (t1.*mfp_print)(s,i);    
    (&t1->*mfp_print)(s,i);
    
    std::cout<<endl;    
    
    std::vector<Test *> vv;
    vv.push_back(&t1);
    vv.push_back(&t2);
    vv.push_back(&t3);
    
    /*******************************************************
    1. i 输出一直是1,此处证明只构造了一次函数对象(bind),后续多次通过调用 bind.operator()(Test *)函数
    2. 对于重载的成员函数,需要强制指定调用重载的那个版本
    */
    std::for_each(vv.begin(),vv.end(), std::bind( (void (Test::*)(const std::string &, int32_t)) &Test::print,_1,s,++i) );
    std::for_each(vv.begin(),vv.end(), std::bind( (C_MFP)&Test::print,_1,s,++i) );
    std::cout<<endl;
    std::for_each(vv.begin(),vv.end(), std::bind( (void (Test::*)()) &Test::print,_1));
    std::cout<<endl;
    /* 
    3. 对于无参数构造调用的函数,可以使用 std::mem_fn,减少占位符
    */
    std::for_each(vv.begin(),vv.end(), std::mem_fn( (void (Test::*)()) &Test::print));
        
    return 0;
}








版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。