std::tr1::bind的使用

 Visual Studio 2008(SP1)版增加了对扩展库TR1的支持,有关TR1的信息,可以上网搜索Boost、TR1。

  TR1中增加了智能指针shared_ptr、函数模板function,以及bind等实用的功能,在我测试bind函数时(有关bind函数,可以上网搜索tr1、bind),出现了类似于“error C2065:“_1”:未声明的标识符”这样的编译错误,怎么也调试不成功,大怒,反复调试,还是不成功,后来发现需要“using namespace std::tr1::placeholders;”。在此写下调试代码:

   1: // compile with: /EHsc 
   2: #include <functional>
   3: #include <algorithm>
   4: #include <iostream>
   5:  
   6: using namespace std::tr1::placeholders;
   7: using std::cout;
   8: using std::endl;
   9: using std::for_each;
  10: using std::tr1::bind;
  11:  
  12: void square(double x)
  13: {
  14:      cout << x << "^2 == " << x * x << endl;
  15: }
  16:  
  17: void product(double x, double y)
  18: {
  19:      cout << x << "*" << y << " == " << x * y << endl;
  20: }
  21:  
  22: int main() 
  23: {
  24:  
  25:      double arg[] = {1, 2, 3};
  26:  
  27:      for_each(&arg[0], &arg[3], square);
  28:      cout << endl;
  29:  
  30:      for_each(&arg[0], &arg[3], bind(product, _1, 2) );
  31:      cout << endl;
  32:  
  33:      for_each(&arg[0], &arg[3], bind(square, _1) );
  34:  
  35:      return 0;
  36: }
  37:  

输出:

1^2 == 1
2^2 == 4
3^2 == 9

1*2 == 2
2*2 == 4
3*2 == 6

1^2 == 1
2^2 == 4
3^2 == 9
请按任意键继续. . .

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值