boost字符串算法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

boost的字符串算法收藏

boost::algorithm简介

boost::algorithm提供了很多字符串算法,包括:大小写转换;去除无效字符;谓词;查找;删除/替换;切割;连接;我们用写例子的方式来了解boost::algorithm能够为我们做些什么。

boost::algorithm学习

#include

using namespace std;

using namespace boost;

一:大小写转换

1 to_upper() 将字符串转为大写

Example:

string str1(" hello 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() 将字符串转为小写,并且赋值给另一个字符串

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' || 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 == "hello 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()

// 将字符串开头以及结尾的空格去掉,示例请参看上面

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 == true

2 istarts_with() 判断一个字符串是否是另外一个字符串的开始串(不区分大小写) Example:

string str1("hello world!");

string str2("Hello");

bool result = istarts_with(str1, str2); // result == true

3 ends_with() 判断一个字符串是否是另外一个字符串的结尾串

4 iends_with() 判断一个字符串是否是另外一个字符串的结尾串(不区分大小写)

5 contains() 判断一个字符串是否包含另外一个字符串

Example:

string str1("hello world!");

string str2("llo");

bool result = contains(str1, str2); // result == true

6 icontains() 判断一个字符串是否包含另外一个字符串(不区分大小写)

7 equals() 判断两个字符串是否相等

8 iequals() 判断两个字符串是否相等(不区分大小写)

9 lexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true (我的boost1.33没有实现?)

10 ilexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true(不区分大小写)(我的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 == true

str1 = "412332211";

result = all(str1, is_123digit); // result == false

四:查找

1 find_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器Example:

相关文档
最新文档