string类型定义--是字符串数组
string类型定义的变量不仅是一个字符串,而是一个字符串数组,可以直接用for循环得到每一位字符。
#include <iostream>
#include<string>
using namespace std;
int main() {
string a;
while (cin >> a) { // 注意 while 处理多个 case
for (int i = 0; i < a.size(); i++)
cout << a[i] << endl;
}
}
运行结果:
(在牛客编译器里面运行的)
reverse函数用法
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
string str="hello,world!";
cout<<str.size()<<endl;
reverse(str.begin(),str.end()); //指明需要翻转的起始和终止范围
cout<<str<<endl;
str="hello,world!";
reverse(&str[0],&str[11]);
cout<<str<<endl;
}
注意第二次翻转时的起始和终止的范围,字符串数组长度为12,下标到11时并不包括最后一个字符。