4.Boost之ref

来源:互联网 发布:游戏鼠标 知乎 编辑:程序博客网 时间:2024/05/21 07:13


1.Boostref,案例:

#include<iostream>

#include<vector>

#include<boost/bind.hpp>

#include<boost/function.hpp>

 

usingnamespacestd;

usingnamespaceboost;

 

voidprint(std::ostream &os,inti)

{

   os <<i <<endl;

}

 

voidmain()

{

   //不可以拷贝的对象可以用ref的方式,入下面cout是系统的对象

   //print中的第一个参数是ostream,引用系统的cout,所以用boost::ref(cout)

   boost::function<void(int)>pt =boost::bind(print,boost::ref(cout),_1);

   vector<int>v;

   v.push_back(11);

   v.push_back(12);

   v.push_back(13);

 

   //下面的pt只需要在传递一个参数即可,通过迭代的方式传入一个参数的

   for_each(v.begin(),v.end(),pt);

 

   std::cin.get();

}

 

 

 

0 0