C++范围for循环

访问for循环,遍历的是数组类可迭代的容器

int i []= {1,2,3,4,5,6,7,8,9};
for(auto c : i)
{
	cout<<c<<endl;
}
#include <string>
string s = "abcdefghijklmn";
for(auto t :s )
	cout<<t<<endl; 

范围for循环遍历的是迭代容器的中的单个元素,如果要修改容器里面的值,创建变量的时候需要使用引用。

string s = "abcdefghijklmn";
for(auto &t : s)
{
	t = toupper(t); // 将s中的全部元素改成大写
}
cout<<s<<endl;
posted @ 2020-03-27 20:47  小白认证  阅读(52)  评论(0)    收藏  举报