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

    1. char & operator[](int n); //通过[]方式取字符
    2. char & at(int n); //通过at方法获取字符
    1. // string字符串存取
    2. void test01()
    3. {
    4. string str = "hello";
    5. cout << "str = " << str << endl;
    6. //1、通过 [] 访问单个字符
    7. for (int i = 0; i < str.size(); i++)
    8. {
    9. cout << str[i] << " ";
    10. }
    11. cout << endl;
    12. //2、通过at方式访问单个字符
    13. for (int i = 0; i < str.size(); i++)
    14. {
    15. cout << str.at(i) << " ";
    16. }
    17. cout << endl;
    18. //修改单个字符
    19. str[0] = 'x';
    20. cout << "str = " << str << endl;
    21. str.at(1) = 'x';
    22. cout << "str = " << str << endl;
    23. }
    24. int main(void)
    25. {
    26. test01();
    27. system("pause");
    28. return 0;
    29. }