boost之string_algo
string_algo是用于处理字符串查找,替换,转换等一系列的字符串算法
前缀i:表示大小写不敏感
后缀_copy:表示不变动输入,返回处理结果的拷贝
后缀_if:表示算法需要一个判断式的谓词函数对象。
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int main()
{
string str("readme.txt");
if (ends_with(str,"txt"))//判断后缀
{
cout << to_upper_copy(str) + " UPPER" <<endl;//大写
}
replace_first(str,"readme","followme");//替换
cout << str <<endl;
vector<char> v(str.begin(),str.end());
vector<char> v2 = to_upper_copy(erase_first_copy(v,"txt"));
for (int i = 0;i < v2.size();++i)
{
cout << v2[i];
}
return 0;
}
1.大小写转换,如上。
2.判断式
2.1判断式(算法),用于子串的匹配
starts_with
ends_with
contains
equals
lexicographcical_compare
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int main()
{
string str("Power Bomb");
if (iends_with(str,"bomb"))
{
cout << "success iends_with" <<endl;
}
if (ends_with(str,"bomb"))
{
cout << "success ends_with" <<endl;
}
if (contains(str,"er"))
{
cout << "success contains" <<endl;
} return 0;
}
2.2.判断式(函数对象)用于字符串之间的比较
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int main()
{
string str1("Samus");
string str2("samus");
if (is_equal()(str1,str2))
{
cout << "is equal" <<endl;
}
if (is_less()(str1,str2))
{
cout << "is less" <<endl;
} return 0;
}
2.3.分类(返回函数对象)用于分类
is_space判断字符是否为空格
is_alnum判断是否为字符或者数字
3.修剪
trim_left,trim_right,trim删除左右两端的空格,使用谓词判断if
#include <iostream>
#include <vector>
#include <string>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int main()
{
format fmt("%s\n");
string str = " samus aran";
cout << fmt % trim_copy(str);
cout << fmt % trim_left_copy(str);
trim_right(str);
cout << fmt % str; string str2 = "2013 Happy lin Year!";
cout << fmt % trim_left_copy_if(str2,is_digit());
cout << fmt % trim_right_copy_if(str2,is_punct()); return 0;
}
4. 查找
find_first 查找字符串第一次出现的位置
find_last 查找字符串最后一次出现的位置
#include <iostream>
#include <vector>
#include <string>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int main()
{
format fmt("%s\n.pos=%d\n");
string str = "Long long ago,there was a king.";
iterator_range<string::iterator> rge;
rge = find_first(str,"long");
cout << fmt % rge % (rge.begin()-str.begin()); return 0;
}
5.替换和删除与查找类似
#include <iostream>
#include <vector>
#include <string>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int main()
{
string str = "Samus beat the monster.\n";
cout << replace_first_copy(str,"Samus","samus");
replace_first(str,"beat","kill");
cout <<str; return 0;
}
6.分割
#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <list>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost; int main()
{
string str = "Smaus,Link.Zelda::Mario_Luigi+zelda";
deque<string> d;
ifind_all(d,str,"zELDA");
if (d.size())
{
cout << "split right" <<endl;
}
list<string> l;
split(l,str,is_any_of(",.:_+"));
for (list<string>::iterator it = l.begin();it != l.end();++it)
{
cout << *it <<endl;
} return 0;
}
7.合并
#include <iostream>
#include <vector>
#include <string> #include <boost/assign.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
using namespace boost::assign; int main()
{
vector<string> v = list_of("liuwei")("linlin")("ceshi");
cout << join(v,"+")<<endl;
struct is_contains_i
{
bool operator()(const string &x)
{
return contains(x,"i");
}
};
cout << join_if(v,"****",is_contains_i());
return 0;
}
boost之string_algo的更多相关文章
- 【Boost】boost::string_algo详解2——find相关函数
来自: https://blog.csdn.net/huang_xw/article/details/8276123 函数声明: template<typename Range1T, typ ...
- (三)Boost库之字符串处理
(三)Boost库之字符串处理 字符串处理一直是c/c++的弱项,string_algo库很好的弥补了这一点. string_algo 库算法命名规则: 前缀i : 有这个前缀表名算法的大小写不 ...
- Boost程序库完全开发指南——深入C++“准”标准库(第3版)
内容简介 · · · · · · Boost 是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉. Boost 由C++标准委员会部分成员所设立的Bo ...
- boost学习目录
Boost之数值转换lexical_cast https://www.cnblogs.com/TianFang/archive/2013/02/05/2892506.html Boost之字符串算法s ...
- Boost 常用的库
boost是一系列C++模板库组成的免费,可移植,开源的程序库.网络上关于boost的文章已经很多. 这里摘记一些库的信息,供自己日后参考. 0.foreach - BOOST_FOREACH ...
- boost::bind 学习
最近学习了太多与MacOS与Iphone相关的东西,因为不会有太多人有兴趣,学习的平台又是MacOS,不太喜欢MacOS下的输入法,所以写下来的东西少了很多. 等我学习的东西慢慢的与平台无关的时 ...
- boost - 正则表达式xpressive
正则表达式是一套处理文本强有力的工具: 它使用一套复杂的语法规则,可以解决文本处理领域的绝大多数问题; 而这些问题通常是字符串算法很难甚至无法解决的. C++98标准中没有内置的正则表达式支持,使得 ...
- boost开发指南
C++确实很复杂,神一样的0x不知道能否使C++变得纯粹和干爽? boost很复杂,感觉某些地方有过度设计和太过于就事论事的嫌疑,对实际开发工作的考虑太过于理想化.学习boost本身就是一个复杂度,有 ...
- Boost总结汇总
从开始接触Boost已经有好几年了,而对它的掌握却难言熟悉,有对它部分的源代码的剖析也是蜻蜓点水.有时间一点点梳理一下吧. 1. 概述 [Boost]C++ Boost库简介[Boost]C++ Bo ...
随机推荐
- Android JNI和NDK学习(03)--动态方式实现JNI(转)
本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3092491.html 前面总结了静态实现JNI的方法,本文介绍如何动态实现J ...
- crontab执行脚本与手动执行结果不一致
反正网上说是环境变量问题,我就直接在脚本第二行加入以下代码: source /etc/profile source ~/.bashrc 问题是解决了!
- UI-10-plist文件及UITableView的高级应用①
课程要点: plist文件的新建与读取 给UITableView设置变化的值 单元格的删除.插入及刷新 plist文件的新建与读取 新建plist Commadn+N,iOS->Resouce- ...
- linux rz xshell
这个命令写好好几次 就是没有记住 放到这里 每次用的时候查一遍 慢慢就记住了~~~ sudo yum install lrzsz -y
- UVA 1363 Joseph's Problem 找规律+推导 给定n,k;求k%[1,n]的和。
/** 题目:Joseph's Problem 链接:https://vjudge.net/problem/UVA-1363 题意:给定n,k;求k%[1,n]的和. 思路: 没想出来,看了lrj的想 ...
- php或js判断网站访问者来自手机或者pc端源码
很多时候也可以通过逻辑程序来进行判断,如PHP.JS是常用的两种识别访问设备类型的常用方法. 原理都是采用识别访问客户端的HTTP_USER_AGENT,然后进行关键字匹配进行确定设备类型,对于伪造H ...
- 第二百节,jQuery EasyUI,Tabs(选项卡)组件
jQuery EasyUI,Tabs(选项卡)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 5.选项卡面板 本节课重点了解 EasyUI 中 Tabs(选项卡)组件的使用方 ...
- Hook length formula 学习笔记 UVALive 6625
最近做到一个关于杨氏矩阵的题目. UVALive 6625 题目大意是用n以内的数填充杨氏矩阵,要求行严格递增,列不严格递增. 求方案数. 数据范围很小,我直接上爆搜,结果TLE了. 后来发现一位学长 ...
- Cobbler简介
Cobbler is a Linux provisioning server that facilitates and automates the network-based system insta ...
- WPF 属性系统 依赖属性之内存占用分析
关于WPF的属性系统园子内有不少这方面的文章.里面大都提到了WPF依赖属性的在内存方面的优化.但是里面大都一笔带过.那么WPF到底是怎么样节约内存的.我们通过WPF属性和普通的CLR属性对比来看一下W ...