C++11 引入了基于范围的迭代写法,能够写出简洁的循环语句;
最常用的std::vector 遍历如前段代码;
新的写法如后段代码;
#include <iostream>
#include<vector>
using namespace std;
int main()
{
std::vector<int> arr(4, 27);
for(std::vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) {
std::cout << *i << std::endl;
}
cout << "----------------------------------" << endl;
std::vector<int> arr2(5, 99);
// & 启用了引用
for(auto &i : arr2) {
std::cout << i << std::endl;
}
getchar();
return 0;
}