1. 程式人生 > >string基本字符序列容器(競賽時常用的使用方法總結)

string基本字符序列容器(競賽時常用的使用方法總結)

互操作 c++ 空字符 算法 對象賦值 cto gpo 添加 清空

C語言只提供了一個char類型用來處理字符,而對於字符串,只能通過字符串數組來處理,而C++STL提供了string基本字符序列容器來處理字符串,可以將其理解為字符串類,它提供了添加,刪除,替換、查找和比較等豐富、簡潔的方法。

下面是在編寫代碼是的具體應用。

  1 //關於C++ STL string基本字符系列容器的學習,看別人的代碼一百遍,不如自己動手寫一遍。
  2 #include <string>
  3 #include <vector> 
  4 #include <iostream>
  5 #include <algorithm>
  6
using namespace std; 7 8 int main() 9 { 10 /*創建string對象 string 加 對象名*/ 11 string s; 12 cout<<s.length()<<endl; 13 /*運行結果 14 0 15 */ 16 17 /*給string對象賦值一般有兩種方式*/ 18 s="hello,C++STL.";//直接賦值 19 //更長用的方法是把一個字符指針賦給一個字符串對象 20 char ss[]="abcd
";//scanf("%s",ss); //註意使用scanf輸入,此處直接賦值 21 s="";//清空字符串 22 if(s.empty()) cout<<"該字符串為空\n"; 23 s=ss;//把整個字符數組賦給string對象 24 cout<<"賦值後為:"<<s<<endl; 25 /*運行結果 26 該字符串為空 27 賦值後為:abcd 28 */ 29 30 /*string對象和字符數組互操作*/ 31 printf(s.c_str());cout<<endl;//
用printf輸出字符串對象,要采用c_str()方法 32 printf("%s\n",ss); 33 cout<<s<<endl;//輸出字符串對象 34 cout<<ss<<endl;//直接使用cout輸出字符串數組 35 /*運行結果 36 abcd 37 abcd 38 abcd 39 abcd 40 */ 41 42 s="hello,C++STL.";//重新直接賦值 43 44 /*在string尾部添加字符或者字符串*/ 45 //要想在string對象尾部添加字符 46 s += a; 47 cout<<s<<endl; 48 //同樣要想在string對象尾部添加字符串 49 s += "bc"; 50 cout<<s<<endl; 51 //也可以使用append()方法 52 s.append("edg"); 53 cout<<s<<endl; 54 /*運行結果 55 hello,C++STL.a 56 hello,C++STL.abc 57 hello,C++STL.abcedg 58 */ 59 60 /*給string對象添加字符,可以使用insert()方法把一個字符插入到叠代器位置之前*/ 61 //定義叠代器 62 string::iterator it1; 63 it1=s.begin(); 64 //把字符插入到第2個字符之前,註意字符位置從0開始計數 65 s.insert(it1+2,h); 66 cout<<s<<endl; 67 /*運行結果 68 hehllo,C++STL.abcedg 69 */ 70 71 /*訪問string對象元素時一般使用下標方式隨機訪問string對象的元素*/ 72 int i; 73 for(i=0;i< s.length();i++){//其中length()方法計算字符串的長度 74 cout<<s[i]<< ; 75 } 76 cout<<endl; 77 /*運行結果 78 h e h l l o , C + + S T L . a b c e d g 79 */ 80 81 /*刪除string對象的元素*/ 82 //清空一個字符串對象直接給他賦一個空字符串即可,即s=""; 83 //要向刪除叠代器所指的那個元素或者一個區間中的所有元素時,使用erase()方法 84 string::iterator it2=s.begin();//定義叠代器變量,指向字符串對象首元素 85 s.erase(it2+2);//刪除第2個元素,元素位置從0開始計數 86 cout<<s<<endl; 87 88 s.erase(it2+13,it2+19);//刪除第13到第18,區間刪除時有區間多加一個單位 89 cout<<s<<endl; 90 /*運行結果 91 hello,C++STL.abcedg 92 hello,C++STL. 93 */ 94 95 /*要想替換string對象的字符,則使用replace()方法*/ 96 //從第0個開始,將連續的5個字符替換為"good",即"hello"替換為"good" 97 s.replace(0,5,"good"); 98 cout<<s<<endl; 99 /*運行結果 100 good,C++STL. 101 */ 102 103 /*要想搜索string對象的元素或子串,則采用find()方法,找到返回下標值,找不到的話,在DEV-C++5.9.2中返回18446744073709551615*/ 104 //查找字符‘C‘ 105 cout<<s.find(C)<<endl; 106 //查找字符串"C++" 107 cout<<s.find("C++")<<endl; 108 //查找字符串"hello" 109 cout<<s.find("hello")<<endl; 110 /*運行結果 111 5 112 5 113 18446744073709551615 114 */ 115 116 /*string對象的比較*/ 117 cout<<s.compare("z")<<endl;//s比"good"字符串小,返回-1 118 cout<<s.compare("good,C++STL.")<<endl;//s與"good"字符串相同,返回0 119 cout<<s.compare("a")<<endl;//s比"good"字符串大,返回1 120 /*運行結果 121 -1 122 0 123 1 124 */ 125 126 /*要想將string對象中一段區間中的元素反向排序,則采用reverse()算法,註意加上頭文件algorithm*/ 127 cout<<"s反向前:\n"; 128 cout<<s<<endl; 129 reverse(s.begin(),s.end()); 130 cout<<"s反向後:\n"; 131 cout<<s<<endl; 132 /*運行結果 133 s反向前: 134 good,C++STL. 135 s反向後: 136 .LTS++C,doog 137 */ 138 139 /*處理二維字符串數組時可將string基本字符序列容器作為vector的元素,從而看作vector向量容器來處理,只不過是元素是string對象*/ 140 vector<string> v; 141 v.push_back("Jack"); 142 v.push_back("Mike"); 143 v.push_back("Tom"); 144 145 cout<<v[0]<<endl; 146 cout<<v[1]<<endl; 147 cout<<v[2]<<endl; 148 cout<<v[0][0]<<endl; 149 cout<<v[1][0]<<endl; 150 cout<<v[2][0]<<endl; 151 cout<<v[2].length()<<endl; 152 /*運行結果 153 Jack 154 Mike 155 Tom 156 J 157 M 158 T 159 3 160 */ 161 return 0; 162 }

string基本字符序列容器(競賽時常用的使用方法總結)