C++之BOOST字符串查找示例

 更新时间:2020年4月25日 17:39  点击:1434

本文实例讲述了C++中BOOST字符串查找的方法,分享给大家供大家参考。具体方法如下:

BOOST  字符串查找示例

复制代码 代码如下:
#include <string> 
#include <iostream> 
#include <algorithm> 
#include <functional> 
#include <boost/algorithm/string/case_conv.hpp> 
#include <boost/algorithm/string/find.hpp> 
 
using namespace std; 
using namespace boost; 
 
int main() 
{   
    cout << "* Find Example *" << endl << endl; 
 
    string str1("abc___cde___efg"); 
    string str2("abc"); 
 
    // find "cde" substring 
    iterator_range<string::iterator> range=find_first( str1, string("cde") ); 
 
    // convert a substring to upper case  
    // note that iterator range can be directly passed to the algorithm 
    to_upper( range ); 
 
    cout << "str1 with upper-cased part matching cde: " << str1 << endl; 
 
    // get a head of the string 
    iterator_range<string::iterator> head=find_head( str1, 3 ); 
    cout << "head(3) of the str1: " << string( head.begin(), head.end() ) << endl; 
 
    // get the tail 
    head=find_tail( str2, 5 ); 
    cout << "tail(5) of the str2: " << string( head.begin(), head.end() ) << endl; 
 
    // char processing 
    char text[]="hello dolly!"; 
    iterator_range<char*> crange=find_last(text,"ll"); 
 
    // transform the range ( add 1 ) 
    transform( crange.begin(), crange.end(), crange.begin(), bind2nd( plus<char>(), 1 ) ); 
    // uppercase the range 
    to_upper( crange ); 
 
    cout << text << endl; 
 
    cout << endl; 
 
    return 0; 
}

boost 判定函数的使用

复制代码 代码如下:
#include <string> 
#include <iostream> 
#include <functional> 
#include <boost/algorithm/string/predicate.hpp> 
#include <boost/algorithm/string/classification.hpp> 
#include <boost/bind.hpp> 

using namespace std; 
using namespace boost; 

int main() 

    cout << "* Predicate Example *" << endl << endl; 
 
    string str1("123xxx321"); 
    string str2("abc"); 
 
    // Check if str1 starts with '123' 
    cout << "str1 starts with \"123\": " <<  
        (starts_with( str1, string("123") )?"true":"false") << endl;  
     
    // Check if str1 ends with '123' 
    cout << "str1 ends with \"123\": " <<  
        (ends_with( str1, string("123") )?"true":"false") << endl;  
 
    // Check if str1 containes 'xxx' 
    cout << "str1 contains \"xxx\": " <<  
        (contains( str1, string("xxx") )?"true":"false") << endl;  
 
    // Check if str2 equals to 'abc' 
    cout << "str2 equals \"abc\": " <<  
        (equals( str2, string("abc") )?"true":"false") << endl;  
 
    // Classification functors and all predicate 
    if ( all(";.,", is_punct() ) ) 
    { 
        cout << "\";.,\" are all punctuation characters" << endl;   
    } 
 
    // Classification predicates can be combined  
    if ( all("abcxxx", is_any_of("xabc") && !is_space() ) ) 
    { 
        cout << "true" << endl; 
    } 
 
    cout << endl; 
 
    return 0; 
}

boost替换示例

复制代码 代码如下:
#include <string> 
#include <iostream> 
#include <iterator> 
//#include <boost/algorithm/string/replace.hpp> 
//#include <boost/algorithm/string/erase.hpp> 
//#include <boost/algorithm/string/case_conv.hpp> 
#include <boost/algorithm/string.hpp> 
 
//Following two includes contain second-layer function. 
//They are already included by first-layer header 
 
//#include <boost/algorithm/string/replace2.hpp> 
//#include <boost/algorithm/string/find2.hpp> 
 
using namespace std; 
using namespace boost; 
 
// uppercase formatter 
/* 
    Convert an input to upper case.  
    Note, that this formatter can be used only on std::string inputs. 
*/ 
inline string upcase_formatter(  
    const iterator_range<string::const_iterator>& Replace ) 

    string Temp(Replace.begin(), Replace.end()); 
    to_upper(Temp); 
    return Temp; 

 
int main() 
{   
    cout << "* Replace Example *" << endl << endl; 
 
    string str1("abc___cde___efg"); 
 
    // Erase 6-9th characters from the string 
    cout << "str1 without 6th to 9th character:" << 
        erase_range_copy( str1, make_iterator_range(str1.begin()+6, str1.begin()+9) ) << endl; 
 
    // Replace 6-9th character with '+++' 
    cout << "str1 with 6th to 9th character replaced with '+++': " <<  
        replace_range_copy(  
            str1, make_iterator_range(str1.begin()+6, str1.begin()+9), "+++" ) << endl; 
 
    cout << "str1 with 'cde' replaced with 'XYZ': "; 
     
    // Replace first 'cde' with 'XYZ'. Modify the input 
    replace_first_copy( ostream_iterator<char>(cout), str1, "cde", "XYZ" ); 
    cout << endl; 
     
    // Replace all '___' 
    cout << "str1 with all '___' replaced with '---': " <<  
        replace_all_copy( str1, "___", "---" ) << endl; 
 
    // Erase all '___' 
    cout << "str1 without all '___': " <<  
        erase_all_copy( str1, "___" ) << endl; 
 
    // replace third and 5th occurrence of _ in str1 
    // note that nth argument is 0-based 
    replace_nth( str1, "_", 4, "+" ); 
    replace_nth( str1, "_", 2, "+" ); 
 
    cout << "str1 with third and 5th occurrence of _ replace: " << str1 << endl; 
 
    // Custom formatter examples 
    string str2("abC-xxxx-AbC-xxxx-abc"); 
 
    // Find string 'abc' ignoring the case and convert it to upper case 
    cout << "Upcase all 'abc'(s) in the str2: " << 
        find_format_all_copy(  
            str2, 
            first_finder("abc", is_iequal()),  
            upcase_formatter ); 
     
    cout << endl; 
 
    return 0; 
}

希望本文所述对大家的C++程序设计有所帮助。

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25
  • C++实现递归函数的方法

    在本篇内容里小编给大家分享了关于C++实现递归函数的教学步骤,需要的朋友跟着参考下。...2020-04-25