【Boost】boost::string_algo详解2——find相关函数
来自: https://blog.csdn.net/huang_xw/article/details/8276123
函数声明:
- template<typename Range1T, typename Range2T>
- iterator_range find_first(Range1T & Input, const Range2T & Search);
- template<typename Range1T, typename Range2T>
- iterator_range find_last(Range1T & Input, const Range2T & Search);
- template<typename Range1T, typename Range2T>
- iterator_range find_nth(Range1T &Input, const Range2T & Search, int Nth);
- template<typename RangeT>
- find_head(RangeT &Input, int N);
- template<typename RangeT>
- find_tail(RangeT & Input, int N);
例子:
- // find_first:【1】查找字符串在输入中第一次出现的位置。
- // find_last: 【2】查找字符串在输入中最后一次出现的位置。
- // find_nth: 【3】查找字符串在输入中的第n次(从0开始计数)出现的位置。
- // find_head: 【4】取一个字符串开头N个字符的字串,相当于substr(0,n);
- // find_tail: 【5】取一个字符串末尾N个字符的字串。
- void test_string_find_string()
- {
- std::string str1("a1234_first_nth_first_nth_");
- boost::iterator_range<std::string::iterator> ir;
- // find_first与ifind_first(不区分大小写,其它同find_first)
- ir = boost::find_first(str1, "first");
- // 1. 通过iterator_range构建字符串
- assert(std::string(ir.begin(), ir.end()) == "first");
- // 2. 查看搜索到的字符串所在位置
- assert(ir.begin() - str1.begin() == 6 && ir.end() - str1.begin() == 6 + 5);
- // 3. 利用iterator_range处理搜索到的字符串
- boost::to_upper(ir);
- assert(str1 == "a1234_FIRST_nth_first_nth_");
- boost::to_lower(ir);
- assert(str1 == "a1234_first_nth_first_nth_");
- // find没有找到的情况
- ir = boost::find_first(str1, "no");
- assert(ir.empty()); // 不存在
- assert(std::string(ir.begin(), ir.end()).empty()); // 不存在,仍可构建一个string
- std::ostringstream osstr;
- osstr << boost::find_first(str1, "_first_");
- assert(osstr.str() == "_first_");
- }
find_token的函数声明
- template<typename RangeT, typename PredicateT>
- iterator_range< typename range_iterator< RangeT >::type >
- find_token(RangeT & Input, PredicateT Pred,
- token_compress_mode_type eCompress = token_compress_off);
find_token的例子
- void test_string_find_token()
- {
- using namespace boost;
- std::string str1("ab1234_first_nth_first_nth_");
- iterator_range<std::string::iterator> ir;
- ir = find_token(str1, is_any_of("irfst"));
- assert(std::string(ir.begin(), ir.end()) == "f");
- ir = find_token(str1, is_any_of("xfirts"), token_compress_off);
- assert(std::string(ir.begin(), ir.end()) == "f");
- ir = find_token(str1, is_any_of("irfst"), token_compress_on);
- assert(std::string(ir.begin(), ir.end()) == "first");
- ir = find_token(str1, is_any_of("fitr "), token_compress_on);
- assert(std::string(ir.begin(), ir.end()) == "fir");
- ir = find_token(str1, is_lower(), token_compress_on);
- assert(std::string(ir.begin(), ir.end()) == "ab");
- }
find_regex的例子
- // 注意加上头文件
- // #include <boost/algorithm/string/regex.hpp>
- // find_regex, find_all_regex
- void test_string_find_regex()
- {
- using namespace boost;
- std::string str1("ab1234_first_nth_first_nth_");
- iterator_range<std::string::iterator> ir;
- regex rx("b[0-9]+_");
- ir = find_regex(str1, rx);
- assert(std::string(ir.begin(), ir.end()) == "b1234_");
- std::string str2("b1_b22_b333_b4444");
- std::vector<std::string> tokens;
- find_all_regex(tokens, str2, rx);
- assert(tokens.size() == 3);
- assert(tokens[0] == "b1_");
- assert(tokens[1] == "b22_");
- assert(tokens[2] == "b333_");
- // 网络上找到的另一个例子
- std::string value = "123a1cxxxxa56c";
- regex pattern("a[0-9]+c");
- iterator_range<std::string::iterator> find_result;
- find_result = algorithm::find_regex(value, pattern);
- assert(!find_result.empty());
- std::vector<std::string> results;
- find_all_regex(results, value, pattern);
- assert("a1c" == results[0]);
- assert("a56c" == results[1]);
- assert(!results.empty());
- value = "10.10.10.10 1.1.1.1";
- boost::regex ip_pattern("(\\d{1, 3}.){3}\\d{1, 3}");
- find_all_regex(results, value, ip_pattern);
- assert("10.10.10.10" == results[0]);
- assert("1.1.1.1" == results[1]);
- assert(!results.empty());
【Boost】boost::string_algo详解2——find相关函数的更多相关文章
- [转] boost::function用法详解
http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...
- boost::function用法详解
要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/func ...
- boost::fucntion 用法详解
转载自:http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost ...
- boost库asio详解1——strand与io_service区别
namespace { // strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行. // io_service不能保证线程安全 boost::a ...
- Boost::bind使用详解
1.Boost::bind 在STL中,我们经常需要使用bind1st,bind2st函数绑定器和fun_ptr,mem_fun等函数适配器,这些函数绑定器和函数适配器使用起来比较麻烦,需要根据是全局 ...
- Boost::split用法详解
工程中使用boost库:(设定vs2010环境)在Library files加上 D:\boost\boost_1_46_0\bin\vc10\lib在Include files加上 D:\boost ...
- 【Boost】boost库asio详解5——resolver与endpoint使用说明
tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...
- boost::algorithm用法详解之字符串关系判断
http://blog.csdn.net/qingzai_/article/details/44417937 下面先列举几个常用的: #define i_end_with boost::iends_w ...
- 【Boost】boost库asio详解3——io_service作为work pool
无论如何使用,都能感觉到使用boost.asio实现服务器,不仅是一件非常轻松的事,而且代码很漂亮,逻辑也相当清晰,这点上很不同于ACE.使用io_service作为处理工作的work pool,可以 ...
随机推荐
- jQuery懒加载插件 – jquery.lazyload.js简单调用
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF-100543G.html 题目传送门 - CF-Gym100543G 题意 你可以对一个字符串进行以下两种操 ...
- 从函数式编程到Ramda函数库(二)
Ramda 基本的数据结构都是原生 JavaScript 对象,我们常用的集合是 JavaScript 的数组.Ramda 还保留了许多其他原生 JavaScript 特性,例如,函数是具有属性的对象 ...
- swap
添加交换分区 SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后将把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理 ...
- ServiceNow在中国还有没有模仿者?
美国版的“ServiceNow”:https://www.servicenow.com 中国版的“ServiceHot” :http://www.itsmcn.com
- 使用Actuator监控
Actuator可能大家非常熟悉,它是springboot提供对应用自身监控,以及对应用系统配置查看等功能. springboot使用actuator的方式非常简单,只需要在项目中加入依赖spring ...
- SpringBoot使用数据库
这一篇简单介绍一下SpringBoot配置数据库的配置(依赖和application.properties),以下全是以本地数据库为例子,具体用户名密码地址都根据实际去修改. Mysql数据库: po ...
- 在addroutes后,$router.options.routes没有更新的问题(手摸手,带你用vue撸后台 读后感)
参照<着手摸手,带你用vue撸后台>一文,本人做了前端的权限判断 https://segmentfault.com/a/1190000009275424 首先就是在addroutes后,$ ...
- AGC 014E.Blue and Red Tree(思路 启发式合并)
题目链接 \(Description\) 给定两棵\(n\)个点的树,分别是由\(n-1\)条蓝边和\(n-1\)条红边组成的树.求\(n-1\)次操作后,能否把蓝树变成红树. 每次操作是,选择当前树 ...
- Centos ATI 显卡安装,“LCD 信号超出范围” 解决方法
centso ATI 显卡驱动安装 centos 版本 32位 6.4 Final ATI 显卡版本:Radeon HD 7400 Series 之前由于很久自己安装了centos显卡,分辨率很低不能 ...