龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

Visual Studio 2010中C++的四大变化(1)

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
在微软即将发布的 Visual Studio 2010 正式版中,其对C++语言做了一些修改,之前51cto也报道过 Visual Studio 2010中关于C++项目的升级 问题,文章则针对C++语言上的一些变化。 Lambda表达式 很多

在微软即将发布的Visual Studio 2010正式版中,其对C++语言做了一些修改,之前51cto也报道过Visual Studio 2010中关于C++项目的升级问题,文章则针对C++语言上的一些变化。

Lambda表达式

很多编程编程语言都支持匿名函数(anonymous function)。所谓匿名函数,就是这个函数只有函数体,而没有函数名。Lambda表达式就是实现匿名函数的一种编程技巧,它为编写匿名函数提供了简明的函数式的句法。同样是Visual Studio中的开发语言,Visual Basic和Visual C#早就实现了对Lambda表达式的支持,终于Visual C++这次也不甘落后,在Visual Studio 2010中添加了对Lambda表达式的支持。

Lambda表达式使得函数可以在使用的地方定义,并且可以在Lambda函数中使用Lambda函数之外的数据。这就为针对集合操作带来了很大的便利。在作用上,Lambda表达式类似于函数指针和函数对象,Lambda表达式很好地兼顾了函数指针和函数对象的优点,却没有它们的缺点。相对于函数指针或是函数对象复杂的语法形式,Lambda表达式使用非常简单的语法就可以实现同样的功能,降低了Lambda表达式的学习难度,避免了使用复杂的函数对象或是函数指针所带来的错误。我们可以看一个实际的例子:

  1. #include "stdafx.h"   
  2. #include <algorithm>   
  3. #include <iostream>   
  4. #include <ostream>   
  5. #include <vector>   
  6.  
  7. using namespace std;   
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])   
  10. {   
  11. vector<int> v;   
  12. for (int i = 0; i < 10; ++i) {   
  13. v.push_back(i);   
  14. }   
  15.  for_each(v.begin(), v.end(), [] (int n) {   
  16. cout << n;   
  17. if (n % 2 == 0) {   
  18. cout << " even ";   
  19. } else {   
  20. cout << " odd ";   
  21. }   
  22. });   
  23. cout << endl;   
  24.  
  25. return 0;   
  26. }  
  27. #include "stdafx.h"  
  28. #include <algorithm> 
  29. #include <iostream> 
  30. #include <ostream> 
  31. #include <vector> 
  32.  
  33. using namespace std;  
  34.  
  35. int _tmain(int argc, _TCHAR* argv[])  
  36. {  
  37. vector<int> v;  
  38. for (int i = 0; i < 10; ++i) {  
  39. v.push_back(i);  
  40. }  
  41.  for_each(v.begin(), v.end(), [] (int n) {  
  42. cout << n;  
  43. if (n % 2 == 0) {  
  44. cout << " even ";  
  45. } else {  
  46. cout << " odd ";  
  47. }  
  48. });  
  49. cout << endl;  
  50.  
  51. return 0;  

这段代码循环遍历输出vector中的每一个数,并判断这个数是奇数还是偶数。我们可以随时修改Lambda表达式而改变这个匿名函数的实现,修改对集合的操作。在这段代码中,C++使用一对中括号“[]”来表示Lambda表达式的开始,其后的”(int n)”表示Lambda表达式的参数。这些参数将在Lambda表达式中使用到。为了体会Lambda表达式的简洁,我们来看看同样的功能,如何使用函数对象实现:

  1. #include "stdafx.h"   
  2. #include <algorithm>   
  3. #include <iostream>   
  4. #include <ostream>   
  5. #include <vector>   
  6. using namespace std;   
  7.  
  8. struct LambdaFunctor {   
  9. void operator()(int n) const {   
  10. cout << n << " ";   
  11. if (n % 2 == 0) {   
  12. cout << " even ";   
  13. } else {   
  14. cout << " odd ";   
  15. }   
  16.  
  17. }   
  18. };   
  19.  
  20. int _tmain(int argc, _TCHAR* argv[])   
  21. {   
  22. vector<int> v;   
  23.  
  24. for (int i = 0; i < 10; ++i) {   
  25. v.push_back(i);   
  26. }   
  27.  
  28. for_each(v.begin(), v.end(), LambdaFunctor());   
  29. cout << endl;   
  30.  
  31. return 0;   
  32. }  
  33. #include "stdafx.h"  
  34. #include <algorithm> 
  35. #include <iostream> 
  36. #include <ostream> 
  37. #include <vector> 
  38. using namespace std;  
  39.  
  40. struct LambdaFunctor {  
  41. void operator()(int n) const {  
  42. cout << n << " ";  
  43. if (n % 2 == 0) {  
  44. cout << " even ";  
  45. } else {  
  46. cout << " odd ";  
  47. }  
  48.  
  49. }  
  50. };  
  51.  
  52. int _tmain(int argc, _TCHAR* argv[])  
  53. {  
  54. vector<int> v;  
  55.  
  56. for (int i = 0; i < 10; ++i) {  
  57. v.push_back(i);  
  58. }  
  59.  
  60. for_each(v.begin(), v.end(), LambdaFunctor());  
  61. cout << endl;  
  62.  
  63. return 0;  
  64. }  


通过比较我们就可以发现,Lambda表达式的语法更加简洁,使用起来更加简单高效。


精彩图集

赞助商链接