温馨提示×

温馨提示×

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

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

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

发布时间:2020-07-11 14:13:53 阅读:390 作者:zxn990 栏目:移动开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

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

#include <iostream>                                                               #include <string>                                                                                                                                                   using namespace std;                                                                                                                                                int main()                                                                        {                                                                                     string s = "Name|Addr|Phone";                                                     char c = '|';                                                                                                                                                       string::size_type i = 0;                                                          string::size_type j = s.find(c, i);                                                                                                                                 while(j != string::npos) {                                                            cout << s.substr(i, j-i) << endl;                                                 i = ++j;                                                                          j = s.find(c, i);                                                             }                                                                                 cout << s.substr(i, s.length()-i) << endl;                                                                                                  return 0;                                                                     } 

问题02:如何使用一组定界符把一个字符串分解成多个片段

    使用basic_string的find_first_of和find_first_not_of成员函数来列举字符串并交替地定位下一个特征符和非特征符。

#include <iostream>                                                               #include <string>                                                                                                                                                   using namespace std;                                                                                                                                                int main()                                                                        {                                                                                     string s = "Name:Addr;Phone";                                                     string d = ":;";                                                                                                                                                    string::size_type i = s.find_first_not_of(d, 0);                                  string::size_type j = s.find_first_of(d, i);                                                                                                                        while(i != string::npos && j != string::npos) {                                       cout << s.substr(i, j-i) << endl;                                                 i = s.find_first_not_of(d, j);                                                    j = s.find_first_of(d, i);                                                    }                                                                                 cout << s.substr(i, s.length()-i) << endl;                                                                                                                          return 0;                                                                     } 

问题03:如何在字符串中查找字符

    使用basic_string的find成员函数,几乎所有以单词"find"开始的函数。每一个函数都有一个basic_string::size_type参数pos,它用来让你能指明查找开始处的索引。函数返回值为basic_string::size_type,如果查找成功,返回值即为目标索引,如果查找失败,返回值为basic_string::npos。

find(); rfind(); find_first_of(); find_first_not_of(); find_last_of(); find_last_not_of(); 

问题04:如何字符串中查找字符串

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

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

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

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

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

isalpha(); isdigit(); isupper(); islower(); isxdigit(); isspace(); iscntrl(); ispunct(); isalnum(); isprint(); isgraph(); 
#include <iostream>                                                               #include <map>                                                                    #include <fstream>                                                                #include <cctype>                                                                 #include <string>                                                                                                                                                   using namespace std;                                                                                                                                                int main()                                                                        {                                                                                     fstream in("sample.txt", ios::in | ios::binary);                                                                                                                    map<string, unsigned int> cmap;                                                                                                                                     char cur;                                                                         while(in.get(cur)) {                      if(isalpha(cur))                                                                      ++cmap["alpha"];                                                              else if(isdigit(cur))                                                                 ++cmap["digit"];                                                              else if(ispunct(cur))                                                                 ++cmap["punct"];                                                              else                                                                                  ++cmap["other"];                                                          }                                                                                                                                                                   map<string, unsigned int>::iterator iter = cmap.begin();                          for( ; iter != cmap.end(); ++iter)                                                    cout << iter->first << " : " << iter->second << endl;                                                                                                           return 0;                                                                     } 

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

    使用流和标准流的格式标志右和左,他们都是定义在<ios>中ios_base的一部分。ios_base类模板中有很多标志可以用来格式化从流读进来的和写入流的数字和文本数据。控制文本对齐的是right和left。它们都是ios_base中的静态成员,都是fmtflags类型。可以使用ios_base::sef来设置格式标志。

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

    但是右对齐如果没有设置右边页面的空白宽度的话就没有意义。为了设置这个宽度,可以使用ios_base::width。

out.width(w); 

    当你用完你设置的格式标志时,你应该做的是清理掉它们。否则,这些标志将影响以后使用流的用户。

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

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

AI

开发者交流群×