? test vim test.cpp
? test g++ test.cpp -o test
? test ./test
original: 6610
auto: 6610
auto&: 1234
? test cat test.cpp
#include<bits/stdc++.h>
using namespace std;
struct node{
int i;
string w;
};
int main(){
vector<node>ns;
ns.clear();
ns.push_back({1,"6610"});
cout<<"original: "<<ns[0].w<<endl;
for(auto it:ns)it.w="1234";
cout<<"auto: "<<ns[0].w<<endl;
for(auto &it:ns)it.w="1234";
cout<<"auto&: "<<ns[0].w<<endl;
}
区别显而易见,可读和可写。
原文:https://www.cnblogs.com/bllovetx/p/11669320.html