Boost.StringAlgo 是 Boost C 库中用于字符串处理的模块提供了一系列高效的字符串算法涵盖查找、替换、分割、修剪、大小写转换等常见操作。其设计目标是补充标准库string和algorithm的不足提供更丰富的功能链式调用支持。/* 大小写转换 void to_upper(T input) //大写改变原来字符串 void to_lower(T input) //小写改变原字符串 T to_upper_copy(T input) 不改变原本字符串 T to_lower_copy(T input) 不改变原本字符串 判断式 starts_with:检测一个字符串是否是另一个的前缀 ends_with:检测一个字符串是否是另一个的后缀 contains:检测一个字符串是否被另一个包含 equals:检测两个字符串是否相等 lexicographical_compare:根据字典序检测一个字符串是否小于另一个 all:检测一个字符串中的所有元素是否满足指定的判断式 除了all所有算法都有一个i前缀的版本 is_equal:类似equals算法比较两个对象是否相等 is_less:比较两个对象是否具有小于关系 is_not_greater:比较两个对象是否具有不大于关系 is_space:字符串是否为空格 is_alnum:字符串是否为字母和数字字符 is_alpha:字符串是否为字母 is_cntrl:字符是否为控制字符 is_digit:是否为数字 is_lower:是否为小写 is_punct:是否为标点符号 is_upper:是否为大写 is_xdigit:是否为十六进制数字 is_any_of:字符是否是参数字符序列中的任意字符 裁剪算法 trim trim_right trim_left trim_copy trim_if trim_copy_if 修剪算法可以删除字符串开始或者结尾部分的空格if接受一个判断式IsSpace 将判断为true的字符删除 查找算法 提供与search()类似的功能但是接口不一样它不是返回一个迭代器查找到的位置 而是使用了boost.range库的iterator_range返回查找到的区间。 iterator_range,它包含了两个迭代器可以使用begin()和end()访问相当于定义了一个容器的子区间 find_first 查找子串第一次在被查找串中出现的位置 find_last 查找子串最后一次在被查找串中的位置 find_nth 查找子串第n次出现在被查找串中的位置 find_head 返回字符串头部n个字符串的位置 find_tail 返回字符串尾部n个字符串的位置 带i前缀忽略大小写 删除/替换 replace/erase_first 替换/删除第一次出现在被查找串中的子串 replace/erase_last 替换/删除最后一次出现在被查找串中的子串 replace/erase_nth 替换/删除第n次出现在被查找串中的子串 replace/erase_all 替换/删除所有的子串 replace/erase_head 替换/删除头部几个字符 replace/erase_tail 替换/删除尾部几个字符 前四组每个都有前缀i后缀copy和组合有四个版本后两组只有后缀copy的两个版本 分割与合并 find_all和spilt把字符串分割并将分割后的字符串复制到指定的容器中 要求必须持有查询结果的复制或引用因此容器元素的类型是string或者iterator_range 容器是vector,list,deque等标准容器 合并算法Join是分割算法split的逆运算把存储在容器中的字符连接成一个新的字符串 并且可以指定连接的分隔符join可带一个if的后缀 */ #includeiostream // string_algo库的头文件 #includeboost/algorithm/string.hpp #includeboost/format.hpp #includeboost/typeof/typeof.hpp #includeset using namespace std; int main() { using namespace boost; using boost::format; //大小写转换 cout 大小写转换 endl; string a adsadA; format fmt(%1% %2%); to_upper(a); //改变原本字符串 cout a endl; string b to_lower_copy(a); fmt% a% b; cout fmt endl; //判断式 返回bool值 cout 判断式 endl; cout starts_with(Hello World, He) endl;//大小写敏感 cout starts_with(Hello World, he) endl; cout istarts_with(Hello World, he) endl; //i忽略大小写 cout ends_with(Hello world, ld) endl;//大小写敏感 cout iends_with(Hello world, Ld) endl;//大小写不敏感 cout contains(Hello World, llo) endl; cout icontains(Hello World, LLO) endl; cout equals(adc, adc) endl; cout iequals(adc, ABc) endl; //判断每一个字符是否全是小写 cout all(Hello, is_lower()) endl; //字典次序是否小于后面的字符串 cout lexicographical_compare(adc, xyz) endl; cout 判断式函数对象 endl; //内容是否等于后面的 i忽略大小写 cout is_equal()(adc, ABc) endl; //内容是否小于后面的 cout is_less()(adc, zBc) endl; //内容是否不大于后面的 cout is_not_greater()(adc, zBc) endl; is_equal eq; cout eq(adc, adc) endl;//与上面的例子一致 cout 分类 endl; cout is_alnum()(1) endl; cout is_alnum()(a) endl; cout is_alnum()(,) endl; cout is_alpha()(a) endl; cout is_digit()(9) endl; cout is_cntrl()(\n) endl; //是否是ADBCDE中的任意一个 cout is_any_of(ABCDE)(F) endl; cout is_xdigit()(Q) endl; //修剪内容 cout 修剪函数 去除左右空白字符 endl; string str Hello ; cout str endl; //直接修改原本的字符串 trim(str); cout str endl; string str2 zhang san ; // 不修改原本的字符串 string str3 trim_copy(str2); cout str2 endl str3 endl; //用一个函数判断要删除的字符是哪一个头尾裁剪 struct is_xing { bool operator()(const charc) { return c *; } }; string str4 ***He***; trim_if(str4, is_xing()); cout str4 endl; /* 查询算法 */ cout 查询算法 endl; string str_algo Hello,hello,hello,Boost; iterator_rangestring::iterator rge; //迭代器区间 //rge ifind_first(str,hello); //rge find_last(str_algo,hello); 最后一个 rge find_first(str_algo, hello); //查找第一次出现 cout rge endl; cout rge.begin() - str_algo.begin() , rge.end() - str_algo.begin() endl; rge find_nth(str_algo, o, 1); //查找第四次出现o的位置 cout rge endl; cout rge.begin() - str_algo.begin() , rge.end() - str_algo.begin() endl; rge find_head(str_algo, 4); //前四个字符出现的位置 次数以0开始的索引 cout rge endl; cout rge.begin() - str_algo.begin() , rge.end() - str_algo.begin() endl; // 替换删除函数 cout 替换删除函数 endl; string str_re Hello,hello,hello,Boost!!!; string str_re1 replace_first_copy(str_re, hello, ***); //修改原本字符串 长度可以不一样 cout str_re1 endl; ierase_first(str_re, hello); // 忽略大小写修改原本字符串 cout str_re endl; erase_all(str_re, o); // 删除所有的o 区分大小写 cout str_re endl; erase_last(str_re, hell); cout str_re endl; // 分割算法 cout 分割算法 endl; string str_sp aaBBccAaxxaa; vectorstring v; //标准容器存放结果 ifind_all(v, str_sp, aa); //for (vectorstring::iterator it v.begin(); it ! v.end(); it) { for(BOOST_AUTO(it,v.begin());it!v.end();it) { cout *it endl; } //找到aa的位置 vectoriterator_rangestring::iterator v2; ifind_all(v2, str_sp, aa); //for (vectorstring::iterator it v.begin(); it ! v.end(); it) { for (BOOST_AUTO(it, v2.begin()); it ! v2.end(); it) { cout it-begin() - str_sp.begin() , it-end() - str_sp.begin() endl; } //使用split分割 cout 使用split分割字符串 endl; string str10 AA**BB*CC-DD; vectorstring Vec; split(Vec, str10, is_any_of(*-)); //分割符指定为*或者- for (auto e : Vec) { cout e endl; } //使用Join连接 cout Join 合并 endl; vectorstring join_vec; join_vec.push_back(stu1); join_vec.push_back(BB); join_vec.push_back(stu2); string join_str join(join_vec, **); cout join_str endl; //加入条件删选包含哪些关键字的字符才加入合并 struct my_contains { bool operator()(const string x) { //return contains(x, A) || contains(x, C); return starts_with(x, stu); } }; join_str join_if(join_vec, **, my_contains()); cout join_str endl; return 0; }