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

问题01:如何把含定界符的字符串分割成多个字符串

创新互联主要从事成都网站制作、成都做网站、外贸营销网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务诸暨,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务: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:如何统计文本文件中不同类型字符的数目

    使用输入流读字符,一次一个,随着你读到的字符,增加相应的统计。判断函数可以使用中定义的字符判断函数。

  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类模板中有很多标志可以用来格式化从流读进来的和写入流的数字和文本数据。控制文本对齐的是right和left。它们都是ios_base中的静态成员,都是fmtflags类型。可以使用ios_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://azwzsj.com/article/geoogs.html