C++ Boost库【2】 --stringalgo字符串算法

Boost.StringAlgo 是 Boost C++ 库中用于字符串处理的模块,提供了一系列高效的字符串算法,涵盖查找、替换、分割、修剪、大小写转换等常见操作。其设计目标是补充标准库(<string><algorithm>)的不足,提供更丰富的功能链式调用支持。

cpp 复制代码
/*
	大小写转换
	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的后缀 
*/
#include<iostream>
// string_algo库的头文件
#include<boost/algorithm/string.hpp>
#include<boost/format.hpp>
#include<boost/typeof/typeof.hpp>
#include<set>
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 char&c) {
			return c == '*';
		}
	};
	string str4 = "***He***";
	trim_if(str4, is_xing());
	cout << str4 << endl;

	/* 查询算法 */
	cout << "查询算法" << endl;
	string str_algo = "Hello,hello,hello,Boost";
	iterator_range<string::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";
	vector<string> v;  //标准容器存放结果
	ifind_all(v, str_sp, "aa");
	//for (vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
	for(BOOST_AUTO(it,v.begin());it!=v.end();++it) {
		cout << *it << endl;
	}

	//找到aa的位置
	vector<iterator_range<string::iterator>> v2;
	ifind_all(v2, str_sp, "aa"); 
	//for (vector<string>::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";
	vector<string> Vec;

	split(Vec, str10, is_any_of("*-"));  //分割符指定为*或者-

	for (auto e : Vec) {
		cout << e << endl;
	}

	//使用Join连接
	cout << "Join 合并" << endl;
	vector<string> 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;
}
相关推荐
BlockWay36 分钟前
WEEX Labs 周度观察:微软-OpenAI 合作调整与AI 多云趋势
大数据·人工智能·算法·安全·microsoft
风筝在晴天搁浅44 分钟前
快手 CodeTop LeetCode 224.基本计算器
数据结构·算法·leetcode
Smoothcloud润云1 小时前
5大功能精修,重构AI算力使用体验!
java·人工智能·windows·算法·重构·编辑器·sublime text
LZZ and MYY1 小时前
RTS 在windows和Linux之间ShareMem
linux·运维·服务器
aningx1 小时前
openSUSE Leap 16.0 运行 sunshine 报错的解决方法
linux
爱学习的徐徐1 小时前
Linux 基础IO
linux·服务器
郝学胜-神的一滴1 小时前
中级OpenGL教程 008:精准控制高光光斑大小与强度
c++·unity·godot·three.js·图形学·opengl·unreal
计算机安禾1 小时前
【算法分析与设计】第41篇:确定性与非确定性多项式时间:P与NP的形式化
算法
xiaobobo33301 小时前
面向对象:linux内核中函数转数据的用法
linux·面向对象·隔离·函数指针绑定
姓刘的哦1 小时前
C++软件架构设计思路
linux