文章目录
范围 for 循环
基于范围的迭代写法,for(变量:对象)表达式
- 对string对象的每个字符做一些操作:
string str("some thing");
for (char c:str)
cout << c << endl;
- 对vector进行遍历:
std::vector<int> arr(5, 100);
for (std::vector<int> iterator i = arr.begin(); i != arr.end(); ++i)
std::cout << *i << std::endl;
OR
for (auto &i:arr)
std::cout << i << std::endl;