【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,可以 ...
随机推荐
- BigInteger的使用
[构造方法] BigInteger(String val) :将 BigInteger 的十进制字符串表示形式转换为 BigInteger. [常用方法] 1)add(BigInteger val): ...
- 给linux服务器添加一块新的磁盘
http://www.linuxidc.com/Linux/2011-02/31868.htm 把硬盘装好后,我们用 fdisk -l 查看下: 图中可以看出 /dev/sdb 是500G,新加的硬盘 ...
- 054 kafka内部机制
一:数据格式与数据存储 1.总结 存储在磁盘文件中(index+log) 顺序读写的 基于offset偏移量来管理数据的(主要是读操作) 由分区器根据key值决定数据分布到哪个分区,默认使用hash ...
- Linux下的Mysql数据库备份+还原
数据库备份: root@debian-mm:/home/debian-mm# mysqldump -u root -p Account > Account.sql Enter password: ...
- css上传图片中等待不可点击效果
<!DOCTYPE html> <html> <head> <title>上传中</title> <style type=" ...
- 正则表达式在python中的简单使用
正则表达式独立与编程语言,基本上所有的编程语言都实现了正则表达式的相关操作.在Python中正则表达式的表现为re模块: import re 其操作有三个方法: my_string = "h ...
- 最短路(bellman)-hdu1217
Dijkstra算法是处理单源最短路径的有效算法,但它局限于边的权值非负的情况,若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的. 这时候,就需要使用其他的算法来求解最 ...
- fetch的总结
&& ) { && ) { }); });
- HDU 1281 棋盘游戏 (枚举+最大匹配)
<题目链接> Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单 ...
- POJ 3104 Drying (经典)【二分答案】
<题目链接> 题目大意: 有一些衣服,每件衣服有一定水量,有一个烘干机,每次可以烘一件衣服,每分钟可以烘掉k滴水.每件衣服没分钟可以自动蒸发掉一滴水,用烘干机烘衣服时不蒸发.问最少需要多少 ...