for(auto &c:s)与for(auto c:s)

在c11标准下可以执行的特殊格式的for循环语句,区别在于引用类型可以改变原来的值

#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

猜你喜欢

转载自blog.csdn.net/weixin_40710375/article/details/80606062
今日推荐