C++11新增了一种循环:基于范围的for循环。这简化了一种常见的循环任务;对数组(或容器类,如vector和array)的每个元素执行相同的操作,例如:

    1. double prices[5] = {4.99, 10.99, 6.87, 7.99, 8.49};
    2. 方法一:
    3. for (double x : prices)
    4. cout << x << std::endl;
    5. 方法二:
    6. for (double &x :prices)
    7. cout << x << std::endl;
    8. 方法三:
    9. for (int x : {1, 2, 3})
    10. cout << x << endl;