#include<iostream>
using namespace std;
int main()
{
string s( "hello world" );
for (auto c:s)
c= 't' ;
cout<<s<<endl; //结果为hello world
for (auto &c:s)
c= 't' ;
cout<<s<<endl; //结果为ttttttttttt
}
for(auto a:b)中b为一个容器,效果是利用a遍历并获得b容器中的每一个值,但是a无法影响到b容器中的元素。
for(auto &a:b)中加了引用符号,可以对容器中的内容进行赋值,即可通过对a赋值来做到容器b的内容填充。
参考:https://blog.csdn.net/weixin_51472673/article/details/122462714