函数原型:
char& operator[](int n);//通过[]的方式取字符
char& at(int n);//通过at的方式取字符
#include<iostream>
using namespace std;
#include<string>
void test01()
{
string str1 = "hello";
//size() 计算字符串的长度
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
//修改
str1[0] = 'a';
str1.at(1) = 'b';
cout << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}