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. 請按任意鍵繼續. . .  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM