Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* C++ 0x test program
- * to compile with g++, use compilation flag -std=gnu++0x
- *
- * Requires at least GCC 4.4 for
- * - auto keyword
- * - initializer lists
- *
- * Requires at least GCC 4.6 for
- * - range-based for
- *
- * GCC 4.6 for Ubuntu: https://launchpad.net/~ubuntu-toolchain-r/+archive/test
- */
- #include <iostream>
- #include <vector>
- // C++03 vector iteration for
- void iter_03 (std::vector<int> &int_vec)
- {
- for (std::vector<int>::const_iterator i = int_vec.begin(); i != int_vec.end(); ++i)
- {
- std::cout << *i << " ";
- }
- }
- // C++0x vector iteration for, using auto keyword
- void iter_0x_auto (std::vector<int> &int_vec)
- {
- for (auto i = int_vec.begin(); i != int_vec.end(); ++i)
- {
- std::cout << *i << " ";
- }
- }
- // C++0x vector iteration for, using range-based for
- void iter_0x_rangebasedfor (std::vector<int> &int_vec)
- {
- for (auto i: int_vec)
- {
- std::cout << i << " ";
- }
- }
- int main()
- {
- // Initializer list
- std::vector<int> int_vec = {1, 2, 3, 5, 7, 11, 13, 17, 19};
- // C++0x style
- for (auto f: {iter_03, iter_0x_auto, iter_0x_rangebasedfor})
- {
- f(int_vec);
- std::cout << "\n";
- }
- // C++03 style
- std::vector<void (*)(std::vector<int>&)> func_vec;
- func_vec.push_back(iter_03);
- func_vec.push_back(iter_0x_auto);
- func_vec.push_back(iter_0x_rangebasedfor);
- for (std::vector<void (*)(std::vector<int>&)>::iterator f = func_vec.begin(); f != func_vec.end(); ++f)
- {
- (*f)(int_vec);
- std::cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement