来自: https://blog.csdn.net/huang_xw/article/details/8276123

函数声明:

  1.  
    template<typename Range1T, typename Range2T>
  2.  
    iterator_range find_first(Range1T & Input, const Range2T & Search);
  3.  
    template<typename Range1T, typename Range2T>
  4.  
    iterator_range find_last(Range1T & Input, const Range2T & Search);
  5.  
    template<typename Range1T, typename Range2T>
  6.  
    iterator_range find_nth(Range1T &Input, const Range2T & Search, int Nth);
  7.  
    template<typename RangeT>
  8.  
    find_head(RangeT &Input, int N);
  9.  
    template<typename RangeT>
  10.  
    find_tail(RangeT & Input, int N);

例子:

  1.  
    // find_first:【1】查找字符串在输入中第一次出现的位置。
  2.  
    // find_last: 【2】查找字符串在输入中最后一次出现的位置。
  3.  
    // find_nth: 【3】查找字符串在输入中的第n次(从0开始计数)出现的位置。
  4.  
    // find_head: 【4】取一个字符串开头N个字符的字串,相当于substr(0,n);
  5.  
    // find_tail: 【5】取一个字符串末尾N个字符的字串。
  6.  
    void test_string_find_string()
  7.  
    {
  8.  
    std::string str1("a1234_first_nth_first_nth_");
  9.  
    boost::iterator_range<std::string::iterator> ir;
  10.  
     
  11.  
    // find_first与ifind_first(不区分大小写,其它同find_first)
  12.  
    ir = boost::find_first(str1, "first");
  13.  
    // 1. 通过iterator_range构建字符串
  14.  
    assert(std::string(ir.begin(), ir.end()) == "first");
  15.  
    // 2. 查看搜索到的字符串所在位置
  16.  
    assert(ir.begin() - str1.begin() == 6 && ir.end() - str1.begin() == 6 + 5);
  17.  
    // 3. 利用iterator_range处理搜索到的字符串
  18.  
    boost::to_upper(ir);
  19.  
    assert(str1 == "a1234_FIRST_nth_first_nth_");
  20.  
    boost::to_lower(ir);
  21.  
    assert(str1 == "a1234_first_nth_first_nth_");
  22.  
     
  23.  
    // find没有找到的情况
  24.  
    ir = boost::find_first(str1, "no");
  25.  
    assert(ir.empty()); // 不存在
  26.  
    assert(std::string(ir.begin(), ir.end()).empty()); // 不存在,仍可构建一个string
  27.  
    std::ostringstream osstr;
  28.  
    osstr << boost::find_first(str1, "_first_");
  29.  
    assert(osstr.str() == "_first_");
  30.  
    }

find_token的函数声明

  1.  
    template<typename RangeT, typename PredicateT>
  2.  
    iterator_range< typename range_iterator< RangeT >::type >
  3.  
    find_token(RangeT & Input, PredicateT Pred,
  4.  
    token_compress_mode_type eCompress = token_compress_off);

find_token的例子

  1.  
    void test_string_find_token()
  2.  
    {
  3.  
    using namespace boost;
  4.  
     
  5.  
    std::string str1("ab1234_first_nth_first_nth_");
  6.  
    iterator_range<std::string::iterator> ir;
  7.  
     
  8.  
    ir = find_token(str1, is_any_of("irfst"));
  9.  
    assert(std::string(ir.begin(), ir.end()) == "f");
  10.  
     
  11.  
    ir = find_token(str1, is_any_of("xfirts"), token_compress_off);
  12.  
    assert(std::string(ir.begin(), ir.end()) == "f");
  13.  
     
  14.  
    ir = find_token(str1, is_any_of("irfst"), token_compress_on);
  15.  
    assert(std::string(ir.begin(), ir.end()) == "first");
  16.  
     
  17.  
    ir = find_token(str1, is_any_of("fitr "), token_compress_on);
  18.  
    assert(std::string(ir.begin(), ir.end()) == "fir");
  19.  
     
  20.  
    ir = find_token(str1, is_lower(), token_compress_on);
  21.  
    assert(std::string(ir.begin(), ir.end()) == "ab");
  22.  
    }

find_regex的例子

    1.  
      // 注意加上头文件
    2.  
      // #include <boost/algorithm/string/regex.hpp>
    3.  
      // find_regex, find_all_regex
    4.  
      void test_string_find_regex()
    5.  
      {
    6.  
      using namespace boost;
    7.  
       
    8.  
      std::string str1("ab1234_first_nth_first_nth_");
    9.  
      iterator_range<std::string::iterator> ir;
    10.  
      regex rx("b[0-9]+_");
    11.  
       
    12.  
      ir = find_regex(str1, rx);
    13.  
      assert(std::string(ir.begin(), ir.end()) == "b1234_");
    14.  
       
    15.  
      std::string str2("b1_b22_b333_b4444");
    16.  
      std::vector<std::string> tokens;
    17.  
      find_all_regex(tokens, str2, rx);
    18.  
      assert(tokens.size() == 3);
    19.  
      assert(tokens[0] == "b1_");
    20.  
      assert(tokens[1] == "b22_");
    21.  
      assert(tokens[2] == "b333_");
    22.  
       
    23.  
      // 网络上找到的另一个例子
    24.  
      std::string value = "123a1cxxxxa56c";
    25.  
      regex pattern("a[0-9]+c");
    26.  
      iterator_range<std::string::iterator> find_result;
    27.  
      find_result = algorithm::find_regex(value, pattern);
    28.  
      assert(!find_result.empty());
    29.  
       
    30.  
      std::vector<std::string> results;
    31.  
      find_all_regex(results, value, pattern);
    32.  
      assert("a1c" == results[0]);
    33.  
      assert("a56c" == results[1]);
    34.  
      assert(!results.empty());
    35.  
       
    36.  
      value = "10.10.10.10 1.1.1.1";
    37.  
      boost::regex ip_pattern("(\\d{1, 3}.){3}\\d{1, 3}");
    38.  
      find_all_regex(results, value, ip_pattern);
    39.  
      assert("10.10.10.10" == results[0]);
    40.  
      assert("1.1.1.1" == results[1]);
    41.  
      assert(!results.empty());
    42.  

【Boost】boost::string_algo详解2——find相关函数的更多相关文章

  1. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

  2. boost::function用法详解

    要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/func ...

  3. boost::fucntion 用法详解

    转载自:http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost ...

  4. boost库asio详解1——strand与io_service区别

    namespace { // strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行. // io_service不能保证线程安全 boost::a ...

  5. Boost::bind使用详解

    1.Boost::bind 在STL中,我们经常需要使用bind1st,bind2st函数绑定器和fun_ptr,mem_fun等函数适配器,这些函数绑定器和函数适配器使用起来比较麻烦,需要根据是全局 ...

  6. Boost::split用法详解

    工程中使用boost库:(设定vs2010环境)在Library files加上 D:\boost\boost_1_46_0\bin\vc10\lib在Include files加上 D:\boost ...

  7. 【Boost】boost库asio详解5——resolver与endpoint使用说明

    tcp::resolver一般和tcp::resolver::query结合用,通过query这个词顾名思义就知道它是用来查询socket的相应信息,一般而言我们关心socket的东东有address ...

  8. boost::algorithm用法详解之字符串关系判断

    http://blog.csdn.net/qingzai_/article/details/44417937 下面先列举几个常用的: #define i_end_with boost::iends_w ...

  9. 【Boost】boost库asio详解3——io_service作为work pool

    无论如何使用,都能感觉到使用boost.asio实现服务器,不仅是一件非常轻松的事,而且代码很漂亮,逻辑也相当清晰,这点上很不同于ACE.使用io_service作为处理工作的work pool,可以 ...

随机推荐

  1. jQuery懒加载插件 – jquery.lazyload.js简单调用

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  2. Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-100543G.html 题目传送门 - CF-Gym100543G 题意 你可以对一个字符串进行以下两种操 ...

  3. 从函数式编程到Ramda函数库(二)

    Ramda 基本的数据结构都是原生 JavaScript 对象,我们常用的集合是 JavaScript 的数组.Ramda 还保留了许多其他原生 JavaScript 特性,例如,函数是具有属性的对象 ...

  4. swap

    添加交换分区 SWAP(交换)分区是一种通过在硬盘中预先划分一定的空间,然后将把内存中暂时不常用的数据临时存放到硬盘中,以便腾出物理内存空间让更活跃的程序服务来使用的技术,其设计目的是为了解决真实物理 ...

  5. ServiceNow在中国还有没有模仿者?

    美国版的“ServiceNow”:https://www.servicenow.com 中国版的“ServiceHot” :http://www.itsmcn.com

  6. 使用Actuator监控

    Actuator可能大家非常熟悉,它是springboot提供对应用自身监控,以及对应用系统配置查看等功能. springboot使用actuator的方式非常简单,只需要在项目中加入依赖spring ...

  7. SpringBoot使用数据库

    这一篇简单介绍一下SpringBoot配置数据库的配置(依赖和application.properties),以下全是以本地数据库为例子,具体用户名密码地址都根据实际去修改. Mysql数据库: po ...

  8. 在addroutes后,$router.options.routes没有更新的问题(手摸手,带你用vue撸后台 读后感)

    参照<着手摸手,带你用vue撸后台>一文,本人做了前端的权限判断 https://segmentfault.com/a/1190000009275424 首先就是在addroutes后,$ ...

  9. AGC 014E.Blue and Red Tree(思路 启发式合并)

    题目链接 \(Description\) 给定两棵\(n\)个点的树,分别是由\(n-1\)条蓝边和\(n-1\)条红边组成的树.求\(n-1\)次操作后,能否把蓝树变成红树. 每次操作是,选择当前树 ...

  10. Centos ATI 显卡安装,“LCD 信号超出范围” 解决方法

    centso ATI 显卡驱动安装 centos 版本 32位 6.4 Final ATI 显卡版本:Radeon HD 7400 Series 之前由于很久自己安装了centos显卡,分辨率很低不能 ...