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
void test_string_case() { // 返回大写拷贝, 原字符串改变 std::string str1("I Don't Know. "); boost::to_upper(str1); std::cout << "str1 = " << str1 << std::endl; // 返回大写拷贝, 原字符串不改变 std::string str2("I Don't Know. "); std::string str3 = boost::to_upper_copy(str2); std::cout << "str2 = " << str2 << std::endl; std::cout << "str3 = " << str3 << std::endl; } void test_string_trim() { std::string str1 = " abc "; std::string str2 = boost::trim_left_copy(str1); std::string str3 = boost::trim_right_copy(str1); std::string str4 = boost::trim_copy(str1); assert(str2=="abc "); assert(str3==" abc"); assert(str4=="abc"); std::string str5 = "0005918580058"; std::string str6 = boost::trim_left_copy_if(str5, boost::is_any_of("0")); std::cout << str6 << std::endl; } void test_string_precidate() { // starts_with assert(boost::starts_with("boost_python-vc100-mt-1_49.dll", "boost")); assert(!boost::starts_with("boost_python-vc100-mt-1_49.dll", "BOOST")); assert(boost::istarts_with("boost_python-vc71-mt-1_33.dll", "BOOST")); // ends_with assert(boost::ends_with("boost_python-vc100-mt-1_49.dll", ".dll")); assert(!boost::ends_with("boost_python-vc100-mt-1_49.dll", ".DLL")); assert(boost::iends_with("boost_python-vc100-mt-1_49.dll", ".DLL")); // contains assert(boost::contains("boost_python-vc100-mt-1_49.dll", "python")); assert(!boost::contains("boost_python-vc100-mt-1_49.dll", "PYTHON")); assert(boost::icontains("boost_python-vc100-mt-1_49.dll", "PYTHON")); // equals assert(boost::equals("boost", "boost")); assert(!boost::equals("boost", "BOOST")); assert(boost::iequals("boost", "BOOST")); // Empty string test assert(boost::starts_with("boost_python-vc100-mt-1_49.dll", "")); assert(boost::ends_with("boost_python-vc100-mt-1_49.dll", "")); assert(boost::contains("boost_python-vc100-mt-1_49.dll", "")); // lexicalgrephical_compare assert(boost::lexicographical_compare("boost_python-vc100-mt-1_49.dll", "boost_system-vc100-mt-1_49.dll")); // all: 如果它的所有元素满足一个给定的通过判断式描述的条件,则这个条件式成立。 assert(boost::all("\x20\t\n\r", boost::is_space())); assert(boost::all("\x20\t\n\r", boost::is_classified(std::ctype_base::space))); assert(boost::all("\x20\t\n\r", boost::is_any_of("\x20\t\n\r"))); assert(boost::all("abcde", boost::is_from_range('a','e'))); assert(boost::all("abcde", boost::is_from_range('a','z'))); assert(!boost::all("abcde", boost::is_from_range('b','c'))); assert(boost::all("abc __ de", boost::is_from_range('a','z') || boost::is_space() || boost::is_any_of("_"))); } void test_string_classify() { // is_space: 字符是否为空格 // is_alnum: 字符是否为字母和数字字符 // is_alpha: 字符是否为字母 // is_cntrl: 字符是否为控制字符 // is_digit: 字符是否为十进制数字 // is_graph: 字符是否为图形字符 // is_lower: 字符是否为小写字符 // is_print: 字符是否为可打印字符 // is_punct: 字符是否为标点符号字符 // is_upper: 字符是否为大写字符 // is_xdigit: 字符是否为十六进制数字 // is_any_of: 字符是否是参数字符序列中的任意字符 // if_from_range 字符是否位于指定区间内,即from <= ch <= to }