1 / 11

Chapter 8

深度學習 C ++. Chapter 8. C++ 字串. C ++ 字串. C ++ 字串為一可 自動調整 字元長度的字元陣列 , 字串長度 沒有限制 C ++ 字串長度就是真正的字元長度 C ++ 字串末尾 無空字元 須加入 string 標頭檔. C++-style string. C ++ 字串物件. 字串物件 字串物件陣列. string a ;. // a 字串沒有任何字元. string b(5 , ’z’);. // b 字串有 5 個 ’z’ 字元. string c = ”cat” ;.

lanai
Télécharger la présentation

Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 深度學習C++ Chapter 8 C++字串

  2. C++ 字串 • C++字串為一可自動調整字元長度的字元陣列, 字串長度沒有限制 • C++字串長度就是真正的字元長度 • C++字串末尾無空字元 • 須加入 string標頭檔 C++-style string

  3. C++ 字串物件 • 字串物件 • 字串物件陣列 string a ; • //a字串沒有任何字元 string b(5 , ’z’); • //b字串有 5個 ’z’字元 string c = ”cat” ; • //c字串有 3個字元,分別為’c’ ’a’ ’t’ string d(”cat”) ; • //d字串同上 string e =’a’ ; • //錯誤,需改成 e = ”a”字串 string f = d ; • //f字串與 d字串相同 string a[10] ; • //10個 C++字串元素所構成的字串陣列

  4. 簡單 C++ 字串操作 (一) • foo = ”孔明”+ ”諸葛亮”; • //錯誤,加法的兩側至少 //要有一個為 C++字串 foo = foo + ”臥龍”; • //foo的末尾加上一字串 foo += ”臥龍”; • //相當於 foo = foo + ”臥龍” • 字串指定:= • 字串合成:+ , += • +與 +=的右側也可以是字元 • string foo ; • foo = ”孔明”;

  5. 簡單 C++ 字串操作 (二) • 字串長度:.size()或.length() • 字元讀取與更改:[] • 產生傳統字串:.c_str() • cout << foo.length() << endl ; • string foo = ”tiger” ; • for ( int i = 0 ; i < foo.size() ; ++i ) { • cout << foo[i] << ’ ’ ; • } • string foo = ”cat” ; • cout << ”>傳統字串:”<< foo.c_str() << endl ; • cout << ”> C++ :”<< foo << endl ;

  6. C++字串邏輯運算子 • C++字串之間可以使用== , != , < , <= , > , >=等邏輯運算子來比較字串的「大小」 • 字串的大小是由字串的第一個字元起依字元在上的順序一一作比較 • 常見的字元大小在 ASCII表內阿拉伯數字 <英文大寫字母 <英文小寫字母

  7. 比較大小:compare • 也可以使用 compare成員函式比較兩字串 string foo( ”the inner light” ) ; string bar( ”The Inner Light” ) ; cout << foo.compare( bar ) << endl ; • 以上如果 foo > bar 則回傳大於零的整數 foo < bar 則回傳小於零的整數 foo == bar 則回傳整數 0

  8. 子字串 • 子字串:由原字串內某段連續的字元所構成的字串。子字串最少包含一個字元,最長可以等於原字串 string foo = ”The Lord of The Rings” ; • 劃底線的部分皆可為子字串

  9. 子字串的取出:substr • substr(p,s): 代表字串在下標p 開始後的連續s 個字元所構成的子字串。若s 被省略則代表下標p 之後的所有字元所構成的子字串 string foo = ”Red Alert !!!” ; cout << foo.substr(4,5) << endl ; cout << foo.substr(4) << endl ; //列印:Alert //列印:Alert !!! • substr 回傳子字串的複製字串,無法當作指定運算子的左值使用 string foo = ”Red Alert !!!” ; foo.substr(4,5) = ”ALERT” ; //錯誤

  10. 子字串的搜尋:find(一) • find(str):由字串起始尋找 str子字串所在的下標位置 string foo = ”Number One” , bar = ”Riker” ;cout << foo.find( ’u’ ) << endl ; cout << foo.find( ”One” )<< endl ; cout << foo.find( bar )<< endl ; //列印:1 //列印:7 //列印:數值// string::npos 以上的 str型別可為字元、傳統字串或C++字串 • 所有的找尋函式都回傳無號整數型別的下標位置 • 若搜尋的子字串不在字串內,則回傳 string::npos之值

  11. 子字串的搜尋:find(二) • find( str , p ):由字串的第 p個字元下標起始尋找 str 子字串所在的下標位置 string foo = ”11001001” , bar = ”100” ; cout << foo.find( ’1’ , 3 )<< endl ; cout << foo.find( ”100” , 3 )<< endl ; cout << foo.find( bar , 3 )<< endl ; //列印:4 //列印:4 //列印:4

More Related