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 == 91*2 == 2
2*2 == 4
3*2 == 61^2 == 1
2^2 == 4
3^2 == 9
请按任意键继续. . .