• C++字符串string类常用操作详解(一)【初始化、遍历、连接】


    代码示例:

    [cpp] view plain copy
     
    1. #include <iostream>  
    2. #include "string"  
    3.   
    4. using namespace std;  
    5.   
    6. //字符串初始化  
    7. void strInit()  
    8. {  
    9.     cout << "字符串初始化:"  <<endl;  
    10.   
    11.     string s1 = "abcdefg";  //初始化方式1  
    12.     string s2("abcdefg");   //初始化方式2  
    13.     string s3 = s2;         //通过拷贝构造函数 初始化s3  
    14.     string s4(7,'s');       //初始化7个s的字符串  
    15.   
    16.     cout << "s1 = "<< s1 << endl;  
    17.     cout << "s2 = "<< s2 << endl;  
    18.     cout << "s3 = "<< s3 << endl;  
    19.     cout << "s4 = "<< s4 << endl;  
    20. }  
    21.   
    22. //字符串遍历  
    23. void strErgo()  
    24. {  
    25.     cout << "字符串遍历:"  <<endl;  
    26.   
    27.     string s1 = "abcdefg";  //初始化字符串  
    28.       
    29.     //通过数组方式遍历  
    30.     cout << "1、通过数组方式遍历:"  <<endl;  
    31.     for (int i = 0; i < s1.length(); i++)  
    32.     {  
    33.         cout << s1[i] << " ";  
    34.     }  
    35.     cout << endl;  
    36.   
    37.     //通过迭代器遍历  
    38.     cout << "2、通过迭代器遍历:"  <<endl;  
    39.     for(string::iterator it = s1.begin(); it!= s1.end(); it++)  
    40.     {  
    41.         cout << *it << " ";  
    42.     }  
    43.     cout << endl;  
    44.   
    45.     //通过at()方式遍历  
    46.     cout << "3、通过at()方式遍历:"  <<endl;  
    47.     for (int i = 0; i < s1.length(); i++)  
    48.     {  
    49.         cout << s1.at(i) << " ";        //此方式可以在越界时抛出异常  
    50.     }  
    51.     cout << endl;  
    52. }  
    53.   
    54. //字符指针和字符串的转换  
    55. void strConvert()  
    56. {  
    57.     cout << "字符指针和字符串的转换:"  <<endl;  
    58.     string s1 = "abcdefg";  //初始化字符串  
    59.   
    60.     cout << "string转换为char*:"  <<endl;  
    61.     //string转换为char*  
    62.     cout << s1.c_str() <<endl;  //s1.c_str()即为s1的char *形式  
    63.   
    64.     cout << "char*获取string内容:"  <<endl;  
    65.     //char*获取string内容  
    66.     char buf[64] = {0};  
    67.     s1.copy(buf, 7);//复制7个元素  
    68.     cout << buf <<endl;  
    69. }  
    70.   
    71. //字符串连接  
    72. void strAdd()  
    73. {  
    74.     cout << "字符串连接:"  <<endl;  
    75.   
    76.     cout << "方式1:"  <<endl;  
    77.     string s1 = "123";  
    78.     string s2 = "456";  
    79.     s1 += s2;  
    80.     cout << "s1 = "<< s1 << endl;  
    81.   
    82.     cout << "方式2:"  <<endl;  
    83.     string s3 = "123";  
    84.     string s4 = "456";  
    85.     s3.append(s4);  
    86.     cout << "s3 = "<< s3 << endl;  
    87. }  
    88. int main()  
    89. {  
    90.     //初始化  
    91.     strInit();  
    92.     cout << endl;  
    93.     //遍历  
    94.     strErgo();  
    95.     cout << endl;  
    96.     //字符指针类型和字符串转换  
    97.     strConvert();  
    98.     cout << endl;  
    99.     //字符串连接  
    100.     strAdd();  
    101.     cout << endl;  
    102.     system("pause");  
    103.     return 0;  
    104. }  

    程序运行结果:

    [plain] view plain copy
     
    1. 字符串初始化:  
    2. s1 = abcdefg  
    3. s2 = abcdefg  
    4. s3 = abcdefg  
    5. s4 = sssssss  
    6.   
    7. 字符串遍历:  
    8. 1、通过数组方式遍历:  
    9. a b c d e f g  
    10. 2、通过迭代器遍历:  
    11. a b c d e f g  
    12. 3、通过at()方式遍历:  
    13. a b c d e f g  
    14.   
    15. 字符指针和字符串的转换:  
    16. string转换为char*:  
    17. abcdefg  
    18. char*获取string内容:  
    19. abcdefg  
    20.   
    21. 字符串连接:  
    22. 方式1:  
    23. s1 = 123456  
    24. 方式2:  
    25. s3 = 123456  
    26.   
    27. 请按任意键继续. . .  
  • 相关阅读:
    技巧篇:如何重写基类的事件
    技巧篇:结合反射技术实现多算法动态加密
    C语言关键字 专一王子:volatile
    第一章 C语言关键字 auto和register
    程序员的进化
    C语言关键字 铁布衫:const
    LINQ简记(3):子句
    windows phone开发之获取屏幕分辨率 和 系统版本
    windows phone开发之 listbox分页加载数据
    Windows phone 微博客户端 开发之项目总结
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/7872545.html
Copyright © 2020-2023  润新知