boost::string_algo详解1

来源:互联网 发布:托米.韦素 知乎 编辑:程序博客网 时间:2024/06/05 05:52

1. 概述

C++98在标准库中提供了字符串标准类std::string. 它有一些成员函数可以查找子串, 访问字符, 可以执行基本的字符串处理功能. 由于std::string符合容器的定义, 也可以把它看做是元素类型为char(或wchar t)的序列容器, 可以使用标准算法来对它进行运算, 但标准算法并不是为字符串处理定制的, 很多时候会显得有些"笨拙".
string_algo库的出现改变了这个尴尬的局面. 它是一个非常全面的字符串算法库, 提
了大量的字符串操作函数, 如大小写无关比较、修剪、特定模式的子串查找等.
string_algo库位于名字空间boost::algorithm, 为了使用string_algo组件, 需要包含头文件<boost/algorithm/string.hpp>。string_algo同样也是一组类的总称,相当于一个sub library了。其位置在boost/algorithm/string目录下,包含的主要内容如下:
case_conv.hpp:  大小写转换,包含 to_lower_copy, to_lower, to_upper_copy, to_upper
classification.hpp: 字符类别,包含 is_classified(std::ctype_base::mask, locale), is_space, is_alnum, is_upper, is_xdigit等方法
cllection_traits.hpp: 一大堆traits类,用于统一char的各种集合类,比如 STL collections, c-style array, null-terminated c-strings等的编程接口。
compare.hpp: 字符串字符的functor,提供了is_equal, is_iequal两个predicates.
concept.hpp: concept定义,包含FinderConcept和FormatterConcept
constants.hpp: 定义了一个enum token_compress_mode_type
erase.hpp: 提供了一组从string中移除字符和子串的方法,什么样的都有。
find.hpp: 提供了一组从string中寻找子串的方法,允许指定各种寻找子串的条作。
finder.hpp:  定义了一组生成string finder的方法
formatter.hpp: 定义了一组生成string formatter的方法
predicate.hpp: 提供了一组predictate, 包含starts_with, ends_withs, contains等
replace.hpp: 提供了一组从string中替换子串的方法
split:hpp: 提供了一组分割子串的方法。
trim.hpp: trim算法。

2. 命名规则

string_algo库中的算法命名遵循了标准库的惯例, 算法名均为小写形式, 并使用不同的前缀或者后缀来区分不同的版本, 如下:
前缀i: 有这个前缀表明算法是大小写不敏感的,否则是大小写敏感的:
后缀copy: 有这个后缀表明算法不变动输入, 返回处理结果的拷贝, 否则算法原地处理, 输入即输出.
后缀if: 有这个后缀表明算法需要一个作为判断式的谓词函数对象,否则使用默认的判断准则. 

3. 算法分类

string_algo库提供的算法共分五大类,如下:
1. 大小写转换
2. 判断式与分类
3. 修剪
4. 查找与替换
5. 分割与合井

4. 例: 3.1-3.3

[cpp] view plaincopyprint?
  1. void test_string_case()  
  2. {  
  3.     // 返回大写拷贝, 原字符串改变  
  4.     std::string str1("I Don't Know. ");  
  5.     boost::to_upper(str1);  
  6.     std::cout << "str1 = " << str1 << std::endl;  
  7.   
  8.     // 返回大写拷贝, 原字符串不改变  
  9.     std::string str2("I Don't Know. ");  
  10.     std::string str3 = boost::to_upper_copy(str2);  
  11.     std::cout << "str2 = " << str2 << std::endl;  
  12.     std::cout << "str3 = " << str3 << std::endl;  
  13. }  
  14.   
  15. void test_string_trim()  
  16. {  
  17.     std::string str1 = "  abc  ";  
  18.     std::string str2 = boost::trim_left_copy(str1);  
  19.     std::string str3 = boost::trim_right_copy(str1);  
  20.     std::string str4 = boost::trim_copy(str1);  
  21.     assert(str2=="abc  ");  
  22.     assert(str3=="  abc");  
  23.     assert(str4=="abc");  
  24.   
  25.     std::string str5 = "0005918580058";  
  26.     std::string str6 = boost::trim_left_copy_if(str5, boost::is_any_of("0"));  
  27.     std::cout << str6 << std::endl;  
  28. }  
  29.   
  30. void test_string_precidate()  
  31. {  
  32.     // starts_with  
  33.     assert(boost::starts_with("boost_python-vc100-mt-1_49.dll""boost"));  
  34.     assert(!boost::starts_with("boost_python-vc100-mt-1_49.dll""BOOST"));  
  35.     assert(boost::istarts_with("boost_python-vc71-mt-1_33.dll""BOOST"));  
  36.   
  37.     // ends_with  
  38.     assert(boost::ends_with("boost_python-vc100-mt-1_49.dll"".dll"));  
  39.     assert(!boost::ends_with("boost_python-vc100-mt-1_49.dll"".DLL"));  
  40.     assert(boost::iends_with("boost_python-vc100-mt-1_49.dll"".DLL"));  
  41.   
  42.     // contains      
  43.     assert(boost::contains("boost_python-vc100-mt-1_49.dll""python"));  
  44.     assert(!boost::contains("boost_python-vc100-mt-1_49.dll""PYTHON"));  
  45.     assert(boost::icontains("boost_python-vc100-mt-1_49.dll""PYTHON"));  
  46.   
  47.     // equals  
  48.     assert(boost::equals("boost""boost"));  
  49.     assert(!boost::equals("boost""BOOST"));  
  50.     assert(boost::iequals("boost""BOOST"));  
  51.   
  52.     // Empty string test  
  53.     assert(boost::starts_with("boost_python-vc100-mt-1_49.dll"""));  
  54.     assert(boost::ends_with("boost_python-vc100-mt-1_49.dll"""));  
  55.     assert(boost::contains("boost_python-vc100-mt-1_49.dll"""));  
  56.   
  57.     // lexicalgrephical_compare  
  58.     assert(boost::lexicographical_compare("boost_python-vc100-mt-1_49.dll""boost_system-vc100-mt-1_49.dll"));  
  59.   
  60.     // all: 如果它的所有元素满足一个给定的通过判断式描述的条件,则这个条件式成立。  
  61.     assert(boost::all("\x20\t\n\r", boost::is_space()));   
  62.     assert(boost::all("\x20\t\n\r", boost::is_classified(std::ctype_base::space)));  
  63.     assert(boost::all("\x20\t\n\r", boost::is_any_of("\x20\t\n\r")));  
  64.     assert(boost::all("abcde", boost::is_from_range('a','e')));  
  65.     assert(boost::all("abcde", boost::is_from_range('a','z')));  
  66.     assert(!boost::all("abcde", boost::is_from_range('b','c')));  
  67.     assert(boost::all("abc __ de", boost::is_from_range('a','z') || boost::is_space() || boost::is_any_of("_")));  
  68. }  
  69.   
  70. void test_string_classify()  
  71. {  
  72. //  is_space: 字符是否为空格  
  73. //  is_alnum: 字符是否为字母和数字字符  
  74. //  is_alpha: 字符是否为字母  
  75. //  is_cntrl: 字符是否为控制字符  
  76. //  is_digit: 字符是否为十进制数字  
  77. //  is_graph: 字符是否为图形字符  
  78. //  is_lower: 字符是否为小写字符  
  79. //  is_print: 字符是否为可打印字符  
  80. //  is_punct: 字符是否为标点符号字符  
  81. //  is_upper: 字符是否为大写字符  
  82. //  is_xdigit: 字符是否为十六进制数字  
  83. //  is_any_of: 字符是否是参数字符序列中的任意字符  
  84. //  if_from_range 字符是否位于指定区间内,即from <= ch <= to  
  85.       
  86. }  
阅读全文
0 0
原创粉丝点击