string 中单个字符存取方式有两种
#include<iostream>
using namespace std;
#include<string>
//字符串存储和修改
void test01()
{
string str1 = "hello";
string str2 = "hello";
//cout << "str1= " << str1 << endl;
//通过中括号 []访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1[i] << " ";
}
cout << endl;
//通过at方式访问单个字符
for (int i = 0; i < str1.size(); i++)
{
cout << str1.at(i) << " ";
}
cout << endl;
//修改单个字符
str1[0] = 'x';
cout << "str1 = " << str1 << endl;
str1.at(1) = 'x';
cout << "str1 = " << str1 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}