18.1.6 string字符存取
可以通过下标或者at成员函数来访问字符串中某个字符。
#include <iostream>
#include <string>
using namespace std;
void test1()
{
string str = "hello";
cout << "str = " << str << endl;
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << ' ';
}
cout << endl;
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << ' ';
}
cout << endl;
//修改单个字符
str[0] = 'x';
cout << "str = " << str << endl;
str.at(1) = 'x';
cout << "str = " << str << endl;
}
int main()
{
test1();
}