真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

Cookbook系列之Cpp:字符串與文本

問題01:如何把含定界符的字符串分割成多個字符串

創(chuàng)新互聯(lián)主要從事成都網站制作、成都做網站、外貿營銷網站建設、網頁設計、企業(yè)做網站、公司建網站等業(yè)務。立足成都服務諸暨,10余年網站建設經驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220

    使用basic_string中的find成員函數依次找到每個定界符,然后使用substr函數把每個子字符串復制出來。

  1. #include                                                                
  2. #include                                                                  
  3.                                                                                   
  4. using namespace std;                                                              
  5.                                                                                   
  6. int main()                                                                        
  7. {                                                                                 
  8.     string s = "Name|Addr|Phone";                                                 
  9.     char c = '|';                                                                 
  10.                                                                                   
  11.     string::size_type i = 0;                                                      
  12.     string::size_type j = s.find(c, i);                                           
  13.                                                                                   
  14.     while(j != string::npos) {                                                    
  15.         cout << s.substr(i, j-i) << endl;                                         
  16.         i = ++j;                                                                  
  17.         j = s.find(c, i);                                                         
  18.     }                                                                             
  19.     cout << s.substr(i, s.length()-i) << endl;           
  20.                                                                                   
  21.     return 0;                                                                     

問題02:如何使用一組定界符把一個字符串分解成多個片段

    使用basic_string的find_first_of和find_first_not_of成員函數來列舉字符串并交替地定位下一個特征符和非特征符。

  1. #include                                                                
  2. #include                                                                  
  3.                                                                                   
  4. using namespace std;                                                              
  5.                                                                                   
  6. int main()                                                                        
  7. {                                                                                 
  8.     string s = "Name:Addr;Phone";                                                 
  9.     string d = ":;";                                                              
  10.                                                                                   
  11.     string::size_type i = s.find_first_not_of(d, 0);                              
  12.     string::size_type j = s.find_first_of(d, i);                                  
  13.                                                                                   
  14.     while(i != string::npos && j != string::npos) {                               
  15.         cout << s.substr(i, j-i) << endl;                                         
  16.         i = s.find_first_not_of(d, j);                                            
  17.         j = s.find_first_of(d, i);                                                
  18.     }                                                                             
  19.     cout << s.substr(i, s.length()-i) << endl;                                    
  20.                                                                                   
  21.     return 0;                                                                     

問題03:如何在字符串中查找字符

    使用basic_string的find成員函數,幾乎所有以單詞"find"開始的函數。每一個函數都有一個basic_string::size_type參數pos,它用來讓你能指明查找開始處的索引。函數返回值為basic_string::size_type,如果查找成功,返回值即為目標索引,如果查找失敗,返回值為basic_string::npos。

  1. find(); 
  2. rfind(); 
  3. find_first_of(); 
  4. find_first_not_of(); 
  5. find_last_of(); 
  6. find_last_not_of(); 

問題04:如何字符串中查找字符串

    你可以使用定義在中的search算法。

問題05:如何比較兩個字符串是否相同

    你可以使用定義在中的equal算法。

問題06:如何統(tǒng)計文本文件中不同類型字符的數目

    使用輸入流讀字符,一次一個,隨著你讀到的字符,增加相應的統(tǒng)計。判斷函數可以使用中定義的字符判斷函數。

  1. isalpha(); 
  2. isdigit(); 
  3. isupper(); 
  4. islower(); 
  5. isxdigit(); 
  6. isspace(); 
  7. iscntrl(); 
  8. ispunct(); 
  9. isalnum(); 
  10. isprint(); 
  11. isgraph(); 
  1. #include                                                                
  2. #include                                                                     
  3. #include                                                                 
  4. #include                                                                  
  5. #include                                                                  
  6.                                                                                   
  7. using namespace std;                                                              
  8.                                                                                   
  9. int main()                                                                        
  10. {                                                                                 
  11.     fstream in("sample.txt", ios::in | ios::binary);                              
  12.                                                                                   
  13.     map cmap;                                               
  14.                                                                                   
  15.     char cur;                                                                     
  16.     while(in.get(cur)) {             
  17.         if(isalpha(cur))                                                          
  18.             ++cmap["alpha"];                                                      
  19.         else if(isdigit(cur))                                                     
  20.             ++cmap["digit"];                                                      
  21.         else if(ispunct(cur))                                                     
  22.             ++cmap["punct"];                                                      
  23.         else                                                                      
  24.             ++cmap["other"];                                                      
  25.     }                                                                             
  26.                                                                                   
  27.     map::iterator iter = cmap.begin();                      
  28.     for( ; iter != cmap.end(); ++iter)                                            
  29.         cout << iter->first << " : " << iter->second << endl;                     
  30.                                                                                   
  31.     return 0;                                                                     

 問題07:如何使你的文本右對齊或左對齊

    使用流和標準流的格式標志右和左,他們都是定義在中ios_base的一部分。ios_base類模板中有很多標志可以用來格式化從流讀進來的和寫入流的數字和文本數據??刂莆谋緦R的是right和left。它們都是ios_base中的靜態(tài)成員,都是fmtflags類型??梢允褂胕os_base::sef來設置格式標志。

  1. out.setf(std::ios_base::right); 

    但是右對齊如果沒有設置右邊頁面的空白寬度的話就沒有意義。為了設置這個寬度,可以使用ios_base::width。

  1. out.width(w); 

    當你用完你設置的格式標志時,你應該做的是清理掉它們。否則,這些標志將影響以后使用流的用戶。

  1. ios_base::fmtflags flags = 
  2.           out.setf(ios_base::left); // setf returns the flags that were already there 
  3.  
  4. out.width(72); 
  5. cout << tmp << endl; 
  6.  
  7. out.flags(flags); // reset to old flags 

網站題目:Cookbook系列之Cpp:字符串與文本
分享鏈接:http://weahome.cn/article/geoogs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部