#include<iostream>
using namespace std;
int main()
{string s( "hello world" );for (auto c:s)c= 't' ;cout<<s<<endl; //結果為hello worldfor (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