#include<iostream>
#include<string.h>
using namespace std;
void test01(){
string str="hello";
//1.通过[]访问单个字符
for(int i=0;i<str.size();i++){
cout<<str[i]<<" ";
}
cout<<endl;
//2.通过at方式访问单个字符
for(int j=0;j<str.size();j++){
cout<<str.at(j)<<" ";
} cout<<endl;
//也可以修改单个字符
str[0]='x';
cout<<str<<endl;
str.at(1)='x';
cout<<str<<endl; }
int main()
{
test01();
return 0;
}
C++ STL string字符存取
![](https://img-home.csdnimg.cn/images/20240711042549.png)