string字符的存取(10)

string中单个字符存取方式有两种

char& operator[](int n); //通过[]方式取字符

char& at(int n); //通过at方法获取字符

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 void test_01()
 6 {
 7     int i = 0;
 8     string str1 = "abcdefghijklmnopqrstuvwxyz";
 9 
10     //
11     for (i; i < str1.size(); i++)
12     {
13         cout << str1[i] << " ";
14     }
15     cout << endl;
16 
17     //修改
18     str1.at(0) = 'A';//注意不是“”而是‘’
19     cout << str1 << endl;
20 }
21 
22 int main(void)
23 {
24     test_01();
25 
26     system("pause");
27     return 0;
28 
29 }

 

posted @ 2020-07-09 12:04  坦率  阅读(245)  评论(0)    收藏  举报