1. /* C++ 0x test program
  2. * to compile with g++, use compilation flag -std=gnu++0x
  3. *
  4. * Requires at least GCC 4.4 for
  5. * - auto keyword
  6. * - initializer lists
  7. *
  8. * Requires at least GCC 4.6 for
  9. * - range-based for
  10. *
  11. * GCC 4.6 for Ubuntu: https://launchpad.net/~ubuntu-toolchain-r/+archive/test
  12. */
  13.  
  14. #include <iostream>
  15. #include <vector>
  16.  
  17. // C++03 vector iteration for
  18. void iter_03 (std::vector<int> &int_vec)
  19. {
  20. for (std::vector<int>::const_iterator i = int_vec.begin(); i != int_vec.end(); ++i)
  21. {
  22. std::cout << *i << " ";
  23. }
  24. }
  25.  
  26. // C++0x vector iteration for, using auto keyword
  27. void iter_0x_auto (std::vector<int> &int_vec)
  28. {
  29. for (auto i = int_vec.begin(); i != int_vec.end(); ++i)
  30. {
  31. std::cout << *i << " ";
  32. }
  33. }
  34.  
  35. // C++0x vector iteration for, using range-based for
  36. void iter_0x_rangebasedfor (std::vector<int> &int_vec)
  37. {
  38. for (auto i: int_vec)
  39. {
  40. std::cout << i << " ";
  41. }
  42. }
  43.  
  44. int main()
  45. {
  46. // Initializer list
  47. std::vector<int> int_vec = {1, 2, 3, 5, 7, 11, 13, 17, 19};
  48.  
  49. // C++0x style
  50. for (auto f: {iter_03, iter_0x_auto, iter_0x_rangebasedfor})
  51. {
  52. f(int_vec);
  53. std::cout << "\n";
  54. }
  55.  
  56. // C++03 style
  57. std::vector<void (*)(std::vector<int>&)> func_vec;
  58. func_vec.push_back(iter_03);
  59. func_vec.push_back(iter_0x_auto);
  60. func_vec.push_back(iter_0x_rangebasedfor);
  61.  
  62. for (std::vector<void (*)(std::vector<int>&)>::iterator f = func_vec.begin(); f != func_vec.end(); ++f)
  63. {
  64. (*f)(int_vec);
  65. std::cout << "\n";
  66. }
  67.  
  68. return 0;
  69. }