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

 更新时间:2014年10月24日 09:27:58   投稿:shichen2014  
这篇文章主要介绍了C++之BOOST字符串查找的方法,实例演示了boost针对字符串的查找、判定及替换等操作,具有一定的实用价值,需要的朋友可以参考下

本文实例讲述了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++程序设计有所帮助。

相关文章

  • 基于排列与组合输出多少中情况详解

    基于排列与组合输出多少中情况详解

    本篇文章对排列与组合输出多少中情况进行了介绍。需要的朋友参考下
    2013-05-05
  • Qt自定义实现一个等待提示Ui控件

    Qt自定义实现一个等待提示Ui控件

    等待样式控件是我们在做UI时出场率还挺高的控件之一,所以这篇文章主要为大家介绍了Qt如何自定义一个好看的等待提示Ui控件,感兴趣的可以了解下
    2024-01-01
  • C++实现LeetCode(71.简化路径)

    C++实现LeetCode(71.简化路径)

    这篇文章主要介绍了C++实现LeetCode(71.简化路径),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C语言实现哈希搜索算法及原理详解

    C语言实现哈希搜索算法及原理详解

    本文主要介绍了C语言实现哈希搜索算法及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • C++程序代码的五大内存分区方式

    C++程序代码的五大内存分区方式

    这篇文章主要介绍了C++程序代码的五大内存分区方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • C++中成员函数和友元函数的使用及区别详解

    C++中成员函数和友元函数的使用及区别详解

    大家好,本篇文章主要讲的是C++中成员函数和友元函数的使用及区别详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • C#桌面应用开发实现番茄定时器

    C#桌面应用开发实现番茄定时器

    本文主要介绍了C#桌面应用开发实现番茄定时器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • C/C++预处理浅析使用形式

    C/C++预处理浅析使用形式

    预处理是指在进行编译的词法扫描和语法分析之前所作的工作。预处理指令指示在程序正式编译前就由编译器进行的操作,可放在程序中任何位置。处理完毕自动进入对源程序的编译。C/C++中的预处理主要包含三种:文件包含、宏定义、条件编译
    2022-09-09
  • C++实现LeetCode(50.求x的n次方)

    C++实现LeetCode(50.求x的n次方)

    这篇文章主要介绍了C++实现LeetCode(50.求x的n次方),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • 浅谈关于C语言中#define的副作用

    浅谈关于C语言中#define的副作用

    这篇文章主要介绍了关于C语言中#define的副作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论