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

发布时间:2020-07-11 14:13:53 作者:zxn990
来源:网络 阅读:383

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

    使用basic_string中的find成员函数依次找到每个定界符,然后使用substr函数把每个子字符串复制出来。

  1. #include <iostream>                                                               
  2. #include <string>                                                                 
  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 <iostream>                                                               
  2. #include <string>                                                                 
  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:如何字符串中查找字符串

    你可以使用定义在<algorithm>中的search算法。

问题05:如何比较两个字符串是否相同

    你可以使用定义在<algorithm>中的equal算法。

问题06:如何统计文本文件中不同类型字符的数目

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

  1. isalpha(); 
  2. isdigit(); 
  3. isupper(); 
  4. islower(); 
  5. isxdigit(); 
  6. isspace(); 
  7. iscntrl(); 
  8. ispunct(); 
  9. isalnum(); 
  10. isprint(); 
  11. isgraph(); 
  1. #include <iostream>                                                               
  2. #include <map>                                                                    
  3. #include <fstream>                                                                
  4. #include <cctype>                                                                 
  5. #include <string>                                                                 
  6.                                                                                   
  7. using namespace std;                                                              
  8.                                                                                   
  9. int main()                                                                        
  10. {                                                                                 
  11.     fstream in("sample.txt", ios::in | ios::binary);                              
  12.                                                                                   
  13.     map<string, unsigned int> 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<string, unsigned int>::iterator iter = cmap.begin();                      
  28.     for( ; iter != cmap.end(); ++iter)                                            
  29.         cout << iter->first << " : " << iter->second << endl;                     
  30.                                                                                   
  31.     return 0;                                                                     

 问题07:如何使你的文本右对齐或左对齐

    使用流和标准流的格式标志右和左,他们都是定义在<ios>中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 
推荐阅读:
  1. DevOps 系列工具之 Puppet 安装与基础配置
  2. 文本处理命令系列——cat

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

string find substring

上一篇:怎么在php中实现域名授权

下一篇:SonataEasyExtendsBundle功能包:概述

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》