for循环在C++ 11中的应用

#include<iostream>
#include<string>
using namespace std;
int test(){
    string s;
    cout<<"请输入一个字符串,可以包含空格:"<<endl;
    getline(cin,s); //读取整行,遇回车符结束
    for(auto &c:s){
        c ='X';
    }
    cout<<s<<endl;
    return 0;
}

 运行结果:

此处c变量有两个特点,1,使用了auto,2,使用了引用,即c是s字符串中的任意字符的别名

发布了156 篇原创文章 · 获赞 36 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/nh5431313/article/details/103973348