免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1675 | 回复: 0
打印 上一主题 下一主题

boost::bind中文文档以及配套范例 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-11 22:51 |只看该作者 |倒序浏览
转自:http://www.allaboutprogram.com/about2074.html
boost::bind的文档,在http://boost.allaboutprogram.com/libs/bind/bind.html
  1. #include <iostream>;
  2. #include <boost/bind.hpp>;
  3. #include <boost/bind/apply.hpp>;

  4. using namespace std;

  5. int add( int i, int j )
  6. {
  7.     return i + j;
  8. }

  9. int add_and_subtract( int i, int j, int k )
  10. {
  11.     return i + j - k;
  12. }

  13. int pass( int& i )
  14. {
  15.     i = 1;
  16.     return i;
  17. }

  18. class functor
  19. {
  20. public:
  21.     int operator()( int i, int j )
  22.     {
  23.         return i + j;
  24.     }
  25.     int operator()( int i, int j, int k )
  26.     {
  27.         return i + j - k;
  28.     }
  29. };

  30. class person
  31. {
  32. public:
  33.     person( const char* s, const char* n ): site( s ), name( n )
  34.     {}
  35.     const char* site;
  36.     const char* name;
  37.     const char* get_name() const
  38.     {
  39.         return name;
  40.     }
  41. };

  42. int main()
  43. {
  44.     cout << boost::bind( add, 5, 5 )( ) << endl;

  45.     int i = 10;
  46.     cout << boost::bind( add, _1, 5 )( i ) << endl;
  47.     cout << boost::bind( add, _1, _1 )( i ) << endl;

  48.     int j = 20;
  49.     cout << boost::bind( add_and_subtract, _1, _1, _2 )( i, j ) << endl;
  50.     cout << boost::bind( add_and_subtract, _1, _1, _2 )( i, j, j, j, j, j, j, j, j ) << endl;

  51.     cout << boost::bind( pass, j )( ) << endl;
  52.     cout << j << endl;
  53.     cout << boost::bind( pass, boost::ref(j) )( ) << endl;
  54.     cout << j << endl;

  55.     cout << boost::bind<int>;( functor(), _1, _1 )( i ) << endl;
  56.     cout << boost::bind<int>;( functor(), _1, _1, _1 )( i ) << endl;
  57.     cout << boost::bind( boost::type<int>;(), functor(), _1, _1, _1 )( i ) << endl;

  58.     person pora( "http://www.allaboutprogram.com/", "pora^-^" );
  59.     cout << boost::bind<const char*>;( person::get_name, pora )() <<endl;
  60.     cout << boost::bind<const char*>;( person::get_name, &pora )() <<endl;
  61.     cout << boost::bind<const char*>;( &person::get_name, pora )() <<endl;
  62.     cout << boost::bind( &person::site, pora )() <<endl;

  63.     cout << boost::bind( add, _1, boost::bind( add_and_subtract, _1, _1, _1 ) )( i ) <<endl;

  64. //    cout << boost::bind<int>;( _1, 5, 5 )( add ) << endl;
  65.     cout << boost::bind( boost::apply<int>;(), _1, 5, 5 )( add ) << endl;

  66.     cout << boost::bind( boost::type<int>;(), add, 5, 5 )( ) << endl;

  67.     return 0;
  68. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP