zoukankan      html  css  js  c++  java
  • 测试functional的bind以及相关功能

    注:在VS2010 UPDATE1下测试通过

     1 /*测试functional的bind以及相关功能*/
     2 
     3 #include <iostream>
     4 #include <functional>
     5 
     6 using namespace std;
     7 using namespace std::placeholders;
     8 
     9 int TestAdd(int a, int b)
    10 {
    11     return a+b;
    12 }
    13 bool TestCompare(int a, int b)
    14 {
    15     return a<b;
    16 }
    17 class TestBindClass
    18 {
    19 public:
    20     bool operator() (const int &a, const int &b) const
    21     {
    22         cout << a << "<" << b << " ? " << ( a<b ? "true" : "false" ) << endl;
    23         return a<b;
    24     }
    25     typedef int first_argument_type;
    26     typedef int second_argument_type;
    27     typedef bool result_type;
    28 };
    29 template <class T> struct greater1 
    30 {
    31     bool operator() (const T& x, const T& y) const 
    32     {
    33         cout << x << ">" << y << " ? " << ( x>y ? "true" : "false" ) << endl;
    34         return x>y;
    35     }
    36     //void operator() (const T& a, const T& b) const {cout << "a<b ? " << ( a<b ? "true" : "false" ) << endl;}
    37     typedef T first_argument_type;
    38     typedef T second_argument_type;
    39     typedef bool result_type;
    40 };
    41 
    42 void main()
    43 {
    44     //test bind function
    45     auto fcn1 = bind(TestAdd, 5, 10);
    46     cout << fcn1() << endl;
    47 
    48     //test bind function
    49     auto fcn2 = bind(TestCompare, _1, 5);
    50     cout << fcn2(4) << endl;
    51 
    52     //test binder1st function
    53     binder1st<greater1<int>> fcn4(greater1<int>(), 6);
    54     cout << fcn4(7) << endl;
    55 
    56     //test binder1st function
    57     binder1st<TestBindClass> fcn3(TestBindClass(), 5);
    58     cout << fcn3(3) << endl;
    59 }
    煮酒论英雄
  • 相关阅读:
    Welcome to my website
    花生壳
    Gentle.NET Attribute
    发布WebFtp 控件(ASP.NET控件,用以web方式进行文件上下传操作)
    发布数据库连接字符串生成工具
    发布语法加亮控件(SyntaxTextBox)
    .NET中现有的 ORM 工具(转)
    发布Oracle存储过程包c#代码生成工具(CodeRobot)
    ASCII码表
    爱上语法高亮控件ICSharpCode.TextEditor ~o~
  • 原文地址:https://www.cnblogs.com/superstargg/p/3712113.html
Copyright © 2011-2022 走看看