C++ string詳解
之所以拋棄char*的字串而選用C++標準程式庫中的string類,是因為他和前者比較起來,不必擔心記憶體是否足夠、字串長度等等,而且作為一個類出現,他集成的操作函式足以完成我們大多數情況下(甚至是100%)的需要,我們可以用 = 進行賦值操作,== 進行比較,+ 做串聯(是不是很簡單?),我們盡可以把它看成是C++的基本資料型別,
1.宣告一個C++字串
a) string s; //生成一個空字串s
b) string s(str) //拷貝建構式 生成str的復制品
c) string s(str,stridx) //將字串str內“始于位置stridx”的部分當作字串的初值
d) string s(str,stridx,strlen) //將字串str內“始于stridx且長度頂多strlen”的部分作為字串的初值
e) string s(cstr) //將C字串作為s的初值
f) string s(chars,chars_len) //將C字串chars前chars_len個字符作為字串s的初值,
g) string s(num,c) //生成一個字串,包含num個c字符
h) string s(beg,end) //以區間beg;end(不包含end)內的字符作為字串s的初值
i) s.~string() //銷毀所有字符,釋放記憶體
2.字串操作函式
a) =,assign() //賦以新值
b) swap() //交換兩個字串的內容
c) +=,append(),push_back() //在尾部添加字符
d) insert() //插入字符
e) erase() //洗掉字符
f) clear() //洗掉全部字符
g) replace() //替換字符
h) + //串聯字串
i) ==,!=,<,<=,>,>=,compare() //比較字串
j) size(),length() //回傳字符數量
k) max_size() //回傳字符的可能最大個數
l) empty() //判斷字串是否為空
m) capacity() //回傳重新分配之前的字符容量
n) reserve() //保留一定量記憶體以容納一定數量的字符
o) [ ], at() //存取單一字符
p) >>,getline() //從stream讀取某值
q) << //將某值寫入stream
r) copy() //將某值賦值為一個C_string
s) c_str() //將內容以C_string回傳
t) data() //將內容以字符陣列形式回傳
u) substr() //回傳某個子字串
v) find() //查找函式
w)begin() end() //提供類似STL的迭代器支持
x) rbegin() rend() //逆向迭代器
y) get_allocator() //回傳配置器
*2.1 C++字串和C字串的轉換
2.2 大小和容量函式
一個C++字串存在三種大小:
現有的字符數,函式是size()和length(),他們等效,Empty()用來檢查字串是否為空,
2.3元素存取
我們可以使用下標運算子[]和函式at()對元素包含的字符進行訪問,
Str[3]; //ok
Str.at(3); //ok
2.4比較函式
C++字串支持常見的比較運算子(>,>=,<,<=,==,!=),甚至支持string與C-string的比較(如 str<”hello”),
1 1) 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 string s("hehe"); 9 cout<<s<<endl; 10 system(“pause”); 11 return 0; 12 } 13 2) 14 #include <string> 15 #include <iostream> 16 using namespace std; 17 18 int main() 19 { 20 char chs[] = "hehe"; 21 string s(chs); 22 cout<<s<<endl; 23 system(“pause”); 24 return 0; 25 } 26 3) 27 #include <string> 28 #include <iostream> 29 using namespace std; 30 31 int main() 32 { 33 char chs[] = "hehe"; 34 string s(chs,1,3); //指定從chs的索引1開始,最后復制3個位元組 35 cout<<s<<endl; 36 system(“pause”); 37 return 0; 38 } 39 4) 40 #include <string> 41 #include <iostream> 42 using namespace std; 43 44 int main() 45 { 46 string s1("hehe"); 47 string s2(s1); 48 cout<<s2<<endl; 49 system(“pause”); 50 return 0; 51 } 52 5) 53 #include <string> 54 #include <iostream> 55 using namespace std; 56 57 int main() 58 { 59 string s1("hehe",2,3); 60 string s2(s1); 61 cout<<s2<<endl; 62 system(“pause”); 63 return 0; 64 } 65 6) 66 #include <string> 67 #include <iostream> 68 using namespace std; 69 70 int main() 71 { 72 char chs[] = "hehe"; 73 string s(chs,3); //將chs前3個字符作為初值構造 74 cout<<s<<endl; 75 system(“pause”); 76 return 0; 77 } 78 7) 79 #include <string> 80 #include <iostream> 81 using namespace std; 82 83 int main() 84 { 85 string s(10,'k'); //分配10個字符,初值都是'k' 86 cout<<s<<endl; 87 system(“pause”); 88 return 0; 89 } 90 //以上是string類實體的構造手段,都很簡單. 91 92 9) 93 //賦新值 94 #include <string> 95 #include <iostream> 96 using namespace std; 97 98 int main() 99 { 100 string s(10,'k'); //分配10個字符,初值都是'k' 101 cout<<s<<endl; 102 s = "hehehehe"; 103 cout<<s<<endl; 104 s.assign("kdje"); 105 cout<<s<<endl; 106 s.assign("fkdhfkdfd",5); //重新分配指定字串的前5的元素內容 107 cout<<s<<endl; 108 system(“pause”); 109 return 0; 110 } 111 10) 112 //swap方法交換 113 #include <string> 114 #include <iostream> 115 using namespace std; 116 117 int main() 118 { 119 string s1 = "hehe"; 120 string s2 = "gagaga"; 121 cout<<"s1 : "<<s1<<endl; 122 cout<<"s2 : "<<s2<<endl; 123 s1.swap(s2); 124 cout<<"s1 : "<<s1<<endl; 125 cout<<"s2 : "<<s2<<endl; 126 system(“pause”); 127 return 0; 128 } 129 11) 130 //+=,append(),push_back()在尾部添加字符 131 #include <string> 132 #include <iostream> 133 using namespace std; 134 135 int main() 136 { 137 string s = "hehe"; 138 s += "gaga"; 139 cout<<s<<endl; 140 s.append("嘿嘿"); //append()方法可以添加字串 141 cout<<s<<endl; 142 s.push_back('k'); //push_back()方法只能添加一個字符... 143 cout<<s<<endl; 144 system(“pause”); 145 return 0; 146 } 147 12) 148 //insert() 插入字符.其實,insert運用好,與其他的插入操作是一樣的. 149 #include <string> 150 #include <iostream> 151 using namespace std; 152 153 int main() 154 { 155 string s = "hehe"; 156 s.insert(0,"頭部"); //在頭部插入 157 s.insert(s.size(),"尾部"); //在尾部插入 158 s.insert(s.size()/2,"中間");//在中間插入 159 cout<<s<<endl; 160 system(“pause”); 161 return 0; 162 } 163 13) 164 #include <string> 165 #include <iostream> 166 using namespace std; 167 168 int main() 169 { 170 string s = "abcdefg"; 171 s.erase(0,1); //從索引0到索引1,即洗掉掉了'a' 172 cout<<s<<endl; 173 //其實,還可以使用replace方法來執行洗掉操作 174 s.replace(2,3,"");//即將指定范圍內的字符替換成"",即變相洗掉了 175 cout<<s<<endl; 176 system(“pause”); 177 return 0; 178 } 179 180 14) 181 //clear() 洗掉全部字符 182 #include <string> 183 #include <iostream> 184 using namespace std; 185 186 int main() 187 { 188 string s = "abcdefg"; 189 cout<<s.length()<<endl; 190 s.clear(); 191 cout<<s.length()<<endl; 192 //使用earse方法變相全洗掉 193 s = "dkjfd"; 194 cout<<s.length()<<endl; 195 s.erase(0,s.length()); 196 cout<<s.length()<<endl; 197 system(“pause”); 198 return 0; 199 } 200 15) 201 //replace() 替換字符 202 #include <string> 203 #include <iostream> 204 using namespace std; 205 206 int main() 207 { 208 string s = "abcdefg"; 209 s.replace(2,3,"!!!!!");//從索引2開始3個位元組的字符全替換成"!!!!!" 210 cout<<s<<endl; 211 system(“pause”); 212 return 0; 213 } 214 16) 215 //==,!=,<,<=,>,>=,compare() 比較字串 216 #include <string> 217 #include <iostream> 218 using namespace std; 219 220 int main() 221 { 222 string s1 = "abcdefg"; 223 string s2 = "abcdefg"; 224 if (s1==s2)cout<<"s1 == s2"<<endl; 225 else cout<<"s1 != s2"<<endl; 226 227 if (s1!=s2)cout<<"s1 != s2"<<endl; 228 else cout<<"s1 == s2"<<endl; 229 230 if (s1>s2)cout<<"s1 > s2"<<endl; 231 else cout<<"s1 <= s2"<<endl; 232 233 if (s1<=s2)cout<<"s1 <= s2"<<endl; 234 else cout<<"s1 > s2"<<endl; 235 236 system(“pause”); 237 return 0; 238 } 239 17) 240 //size(),length() 回傳字符數量 241 #include <string> 242 #include <iostream> 243 using namespace std; 244 245 int main() 246 { 247 string s = "abcdefg"; 248 cout<<s.size()<<endl; 249 cout<<s.length()<<endl; 250 251 system(“pause”); 252 return 0; 253 } 254 18) 255 //max_size() 回傳字符的可能最大個數 256 #include <string> 257 #include <iostream> 258 using namespace std; 259 260 int main() 261 { 262 string s = "abcdefg"; 263 cout<<s.max_size()<<endl; 264 265 system(“pause”); 266 return 0; 267 } 268 19) 269 //empty() 判斷字串是否為空 270 #include <string> 271 #include <iostream> 272 using namespace std; 273 274 int main() 275 { 276 string s ; 277 if (s.empty()) 278 cout<<"s 為空."<<endl; 279 else 280 cout<<"s 不為空."<<endl; 281 282 s = s + "abcdefg"; 283 if (s.empty()) 284 cout<<"s 為空."<<endl; 285 else 286 cout<<"s 不為空."<<endl; 287 288 system(“pause”); 289 return 0; 290 } 291 20) 292 // [ ], at() 存取單一字符 293 #include <string> 294 #include <iostream> 295 using namespace std; 296 297 int main() 298 { 299 string s = "abcdefg1111"; 300 301 cout<<"use []:"<<endl; 302 for(int i=0; i<s.length(); i++) 303 { 304 cout<<s[i]<<endl; 305 } 306 cout<<endl; 307 308 cout<<"use at():"<<endl; 309 for(int i=0; i<s.length(); i++) 310 { 311 cout<<s.at(i)<<endl; 312 } 313 cout<<endl; 314 315 system(“pause”); 316 return 0; 317 } 318 21) 319 #include <string> 320 #include <iostream> 321 using namespace std; 322 323 int main() 324 { 325 string s = "abcdefg1111"; 326 327 const char * chs1 = s.c_str(); 328 const char * chs2 = s.data(); 329 330 cout<<"use at():"<<endl; 331 int i; 332 for(i=0; i<s.length(); i++) 333 { 334 cout<<"c_str() : "<<chs1[i]<<endl; 335 cout<<"data() : "<<chs2[i]<<endl; 336 } 337 cout<<"c_str() : "<<chs1<<endl; 338 cout<<"data() : "<<chs2<<endl; 339 cout<<endl; 340 341 system(“pause”); 342 return 0; 343 } 344 22) 345 // substr() 回傳某個子字串 346 #include <string> 347 #include <iostream> 348 using namespace std; 349 350 int main() 351 { 352 string s = "abcdefg1111"; 353 354 string str = s.substr(5,3);//從索引5開始3個位元組 355 cout<<str<<endl; 356 357 system(“pause”); 358 return 0; 359 } 360 23) 361 // find 查找函式 362 #include <string> 363 #include <iostream> 364 using namespace std; 365 366 int main() 367 { 368 string s = "abcdefg1111"; 369 string pattern = "fg"; 370 string::size_type pos; 371 pos = s.find(pattern,0); //從索引0開始,查找符合字串"fg"的頭索引 372 cout<<pos<<endl; 373 string str = s.substr(pos,pattern.size()); 374 cout<<str<<endl; 375 system(“pause”); 376 return 0; 377 } 378 24) 379 // begin() end() 提供類似STL的迭代器支持 380 #include <string> 381 #include <iostream> 382 using namespace std; 383 384 int main() 385 { 386 string s = "abcdefg1111"; 387 for(string::iterator iter = s.begin(); iter!=s.end(); iter++) 388 { 389 cout<<*iter<<endl; 390 } 391 cout<<endl; 392 393 system(“pause”); 394 return 0; 395 }cstring代碼實體
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/89832.html
標籤:C++
上一篇:C/C++中new的使用規則
