关注小韩 轻松编程 一起由浅入深
#include<iostream>
using namespace std;
#include<string>
//总结:string字符串中单个字符存取有两种方法 []或at
//string中单个字符存取
void test()
{
string str = "hello";
//cout << "str=" << str << endl; //字符串输出
//1. 通过 [] 访问单个字符
for (int i = 0; i < str.size(); i++)
cout << str[i] << " ";
cout << endl;
//2. 通过at方式访问单个字符
for (int i = 0; i < str.size(); i++)
cout << str.at(i) << " ";
cout << endl;
//修改单个字符
str[0] = 'x'; //利用[]修改
cout << str << endl; //xello
str.at(1) = 's'; //利用at修改
cout << str << endl; //xsllo
}
int main()
{
test();
return 0;
}
有疑问欢迎留言哦!