




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、boost的字符串算法 收藏 boost:algorithm简介boost:algorithm提供了很多字符串算法,包括: 大小写转换; 去除无效字符; 谓词; 查找; 删除/替换; 切割; 连接; 我们用写例子的方式来了解boost:algorithm能够为我们做些什么。 boost:algorithm学习#include <boost/algorithm/string.hpp>using namespace std;using namespace boost; 一:大小写转换1 to_upper() 将字符串转为大写Example:string str1(" hell
2、o world! ");to_upper(str1); / str1 = " HELLO WORLD! "2 to_upper_copy() 将字符串转为大写,并且赋值给另一个字符串Example:string str1(" hello world! ");string str2;str2 = to_upper_copy(str1); / str2 = " HELLO WORLD! "3 to_lower() 将字符串转为小写Example:参看to_upper()4 to_lower_copy() 将字符串转为小写,并且赋
3、值给另一个字符串Example:参看to_upper_copy()二:Trimming(整理,去掉首尾的空格字符)1 trim_left() 将字符串开头的空格去掉Example:string str1(" hello world! ");trim_left(str1); / str1 = "hello world! "2 trim_left_if() 将字符串开头的符合我们提供的“谓词”的特定字符去掉Example:bool NotH(const char &ch) if(ch = ' ' | ch = 'H'
4、| ch = 'h') return true; else return false;.string str1(" hello world! ");trim_left_if(str1, NotH); / str1 = "ello world! "3 trim_left_copy() 将字符串开头的空格去掉,并且赋值给另一个字符串Example:string str1(" hello world! ");string str2;str2 = trim_left_copy(str1); / str2 = "hel
5、lo world! "4 trim_left_copy_if() 将字符串开头的符合我们提供的“谓词”的特定字符去掉,并且赋值给另一个字符串Example:string str1(" hello world! ");string str2;str2 = trim_left_copy_if(str1, NotH); / str2 = "ello world! "/ 将字符串结尾的空格去掉,示例请参看上面5 trim_right_copy_if()6 trim_right_if()7 trim_right_copy()8 trim_right()/
6、 将字符串开头以及结尾的空格去掉,示例请参看上面9 trim_copy_if()10 trim_if()11 trim_copy()12 trim()三:谓词1 starts_with() 判断一个字符串是否是另外一个字符串的开始串Example:string str1("hello world!");string str2("hello");bool result = starts_with(str1, str2); / result = true2 istarts_with() 判断一个字符串是否是另外一个字符串的开始串(不区分大小写)Example
7、:string str1("hello world!");string str2("Hello");bool result = istarts_with(str1, str2); / result = true3 ends_with() 判断一个字符串是否是另外一个字符串的结尾串4 iends_with() 判断一个字符串是否是另外一个字符串的结尾串(不区分大小写)5 contains() 判断一个字符串是否包含另外一个字符串Example:string str1("hello world!");string str2("l
8、lo");bool result = contains(str1, str2); / result = true6 icontains() 判断一个字符串是否包含另外一个字符串(不区分大小写)7 equals() 判断两个字符串是否相等8 iequals() 判断两个字符串是否相等(不区分大小写)9 lexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true (我的boost1.33没有实现?)10 ilexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true(不区分大小写)(
9、我的boost1.33没有实现?)11 all() 判断字符串中的所有字符是否全部满足这个谓词Example:bool is_123digit(const char &ch) if(ch = '1' | ch = '2' | ch = '3') return true; else return false;.string str1("12332211");bool result = all(str1, is_123digit); / result = truestr1 = "412332211"re
10、sult = all(str1, is_123digit); / result = false四:查找1 find_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器Example:char ToUpper(char &ch) if(ch <= 'z' && ch >= 'a') return ch + 'A'-'a' else return ch;.string str1("hello dolly!");iterator_r
11、ange<string:iterator> result = find_first(str1,"ll");transform( result.begin(), result.end(), result.begin(), ToUpper ); / str1 = "heLLo dolly!"2 ifind_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)3 find_last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器4 ifind_
12、last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)5 find_nth() 找到第n个匹配的子串(计算从0开始)Example:string str1("hello dolly!");iterator_range<string:iterator> result = find_nth(str1,"ll", 1);transform( result.begin(), result.end(), result.begin(), ToUpper ); / str1 = "hel
13、lo doLLy!"6 ifind_nth() 找到第n个匹配的子串(计算从0开始)(不区分大小写)7 find_head() 找到字符串的前n个字节Example:string str1("hello dolly!");iterator_range<string:iterator> result = find_head(str1,5);transform( result.begin(), result.end(), result.begin(), ToUpper ); / str1 = "HELLO dolly!"8 find_t
14、ail() 找到字符串的后n个字节9 find_token() 找到符合谓词的串Example:char Add1(const char &ch) return ch+1;.string str1("hello 1 world!");iterator_range<string:iterator> result = find_token(str1,is_123digit);transform( result.begin(), result.end(), result.begin(), Add1 ); / str1 = "hello 2 world
15、!");10 find_regex() 匹配正则表达式Example:(等稍候了解了boost的正则表达式后再给出)11 find() 使用自己写的查找函数Example:iterator_range<string:iterator>MyFinder1( std:string:iterator begin, std:string:iterator end ) std:string:iterator itr; for(itr = begin;itr!=end;itr+) if(*itr) = '1') std:string:iterator preitr =
16、 itr; iterator_range<string:iterator> ret(preitr, +itr); return ret; return iterator_range<string:iterator>(); / boost自己也提供了很多Finder.string str1("hello 1 world!");iterator_range<string:iterator> result = find(str1,MyFinder1);transform( result.begin(), result.end(), result
17、.begin(), Add1 ); / str1 = "hello 2 world!");五:删除/替换1 replace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串Example:string str1("hello world!");replace_first(str1, "hello", "Hello"); / str1 = "Hello world!"2 replace_first_copy() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,
18、并且赋值给另一个字符串Example:string str1("hello world!");string str2;str2 = replace_first_copy(str1, "hello", "Hello"); / str2 = "Hello world!"3 ireplace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串(不区分大小写)4 ireplace_first_copy() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋值给另一个字符串(不区分大小
19、写)5 erase_first() 从头找到第一个匹配的字符串,将其删除Example:string str1("hello world!");erase_first(str1, "llo"); / str1 = "He world!"6 erase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串Example:string str1("hello world!");string str2;str2 = erase_first_copy(str1, "llo&quo
20、t;); / str2 = "He world!"7 ierase_first() 从头找到第一个匹配的字符串,将其删除(不区分大小写)8 ierase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串(不区分大小写)/ 与上面类似,不过是从字符串尾开始替换9 replace_last()10 replace_last_copy()11 ireplace_last()12 ireplace_last_copy()13 erase_last()14 erase_last_copy()15 ierase_last()16 ierase_la
21、st_copy()/ 与上面类似,不过是从字符串第n个匹配的开始替换17 replace_nth() Example:string str1("hello world!");replace_nth(str1, "o", 1, "O"); / str1 = "hello wOrld!"18 replace_nth_copy()19 ireplace_nth()20 ireplace_nth_copy()21 erase_nth()22 erase_nth_copy()23 ierase_nth()24 ierase_
22、nth_copy()/ 与上面类似,不过是将所有的匹配字符串替换25 replace_all()26 replace_all_copy()27 ireplace_all()28 ireplace_all_copy()29 erase_all()30 erase_all_copy()31 ierase_all()32 ierase_all_copy()33 replace_head() 替换前n个字符Example:string str1("hello world!");replace_head(str1, 5, "HELLO"); / str1 = &q
23、uot;HELLO world!"34 replace_head_copy() 替换前n个字符,并且赋值给另一个字符串Example:string str1("hello world!");string str2;str2 = replace_head_copy(str1, 5, "HELLO"); / str2 = "HELLO world!"35 erase_head() 删除前n个字符Example:string str1("hello world!");erase_head(str1, 5); /
24、 str1 = " world!"36 erase_head_copy() 删除前n个字符,并且赋值给另一个字符串Example: string str1("hello world!");string str2;str2 = erase_head_copy(str1, 5); / str2 = " world!"/ 与上面类似(替换/删除后n个字符)37 replace_tail() 38 replace_tail_copy() 39 erase_tail() 40 erase_tail_copy()/ 与正则表示式相关,稍后了解。4
25、1 replace_regex() 42 replace_regex_copy() 43 erase_regex() 44 erase_regex_copy() 45 replace_all_regex() 46 replace_all_regex_copy() 47 erase_all_regex() 48 erase_all_regex_copy() / 不是很清楚,稍后了解49 find_format() 50 find_format_copy() 51 find_format_all() 52 find_format_all_copy()六:切割1 find_all() 查找所有匹配的
26、值,并且将这些值放到给定的容器中Example:string str1("hello world!");std:vector<std:string> result;find_all(result, str1, "l"); / result = 3("l","l","l")2 ifind_all() 查找所有匹配的值,并且将这些值放到给定的容器中(不区分大小写)3 find_all_regex() 与正则表达式相关,稍后了解4 split() 按照给定的谓词切割字符串,并且把切割后的值放入到给定的容器中Example:class SplitNotThisCharpublic: SplitNotThisChar(const char ch) : m_char(ch) b
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 通信网络技术LTE知识点试题及答案
- 学校2025年年终工作总结(5篇)
- 农村住宅环境改善施工合同书
- 我与考试的信息系统监理师备考试题及答案
- 防火墙的基本配置与管理试题及答案
- 协议书与合同的法律效益
- 行政组织的社会网络分析与管理试题及答案
- 行政组织中利益协调的试题及答案
- 网络技术面临的挑战与机遇试题及答案
- 重要网络术语的定义与考察试题及答案
- 2025江苏中考:化学必背知识点
- 2024-2025学年度广东省广州市南沙区中考英语一模试卷(含解析)
- 高标准农田项目规划设计方案
- 混凝土预制构件项目可行性研究报告
- 2025年公牛插座市场调研报告
- 无人机拍摄培训课件
- 特岗教师科学试题及答案
- 抖音员工合同协议
- 银行培训中心管理制度
- 锂电池基础知识培训单选题100道及答案
- 2025年陕西省八年级中考三模生物试题(原卷版+解析版)
评论
0/150
提交评论