最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年10月24日 09:27:58   投稿:shichen2014  
這篇文章主要介紹了C++之BOOST字符串查找的方法,實例演示了boost針對字符串的查找、判定及替換等操作,具有一定的實用價值,需要的朋友可以參考下

本文實例講述了C++中BOOST字符串查找的方法,分享給大家供大家參考。具體方法如下:

BOOST  字符串查找示例

復(fù)制代碼 代碼如下:
#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 判定函數(shù)的使用

復(fù)制代碼 代碼如下:
#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替換示例

復(fù)制代碼 代碼如下:
#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++程序設(shè)計有所幫助。

相關(guān)文章

  • 基于排列與組合輸出多少中情況詳解

    基于排列與組合輸出多少中情況詳解

    本篇文章對排列與組合輸出多少中情況進(jìn)行了介紹。需要的朋友參考下
    2013-05-05
  • Qt自定義實現(xiàn)一個等待提示Ui控件

    Qt自定義實現(xiàn)一個等待提示Ui控件

    等待樣式控件是我們在做UI時出場率還挺高的控件之一,所以這篇文章主要為大家介紹了Qt如何自定義一個好看的等待提示Ui控件,感興趣的可以了解下
    2024-01-01
  • C++實現(xiàn)LeetCode(71.簡化路徑)

    C++實現(xiàn)LeetCode(71.簡化路徑)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(71.簡化路徑),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言實現(xiàn)哈希搜索算法及原理詳解

    C語言實現(xiàn)哈希搜索算法及原理詳解

    本文主要介紹了C語言實現(xiàn)哈希搜索算法及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • C++程序代碼的五大內(nèi)存分區(qū)方式

    C++程序代碼的五大內(nèi)存分區(qū)方式

    這篇文章主要介紹了C++程序代碼的五大內(nèi)存分區(qū)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • C++中成員函數(shù)和友元函數(shù)的使用及區(qū)別詳解

    C++中成員函數(shù)和友元函數(shù)的使用及區(qū)別詳解

    大家好,本篇文章主要講的是C++中成員函數(shù)和友元函數(shù)的使用及區(qū)別詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • C#桌面應(yīng)用開發(fā)實現(xiàn)番茄定時器

    C#桌面應(yīng)用開發(fā)實現(xiàn)番茄定時器

    本文主要介紹了C#桌面應(yīng)用開發(fā)實現(xiàn)番茄定時器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • C/C++預(yù)處理淺析使用形式

    C/C++預(yù)處理淺析使用形式

    預(yù)處理是指在進(jìn)行編譯的詞法掃描和語法分析之前所作的工作。預(yù)處理指令指示在程序正式編譯前就由編譯器進(jìn)行的操作,可放在程序中任何位置。處理完畢自動進(jìn)入對源程序的編譯。C/C++中的預(yù)處理主要包含三種:文件包含、宏定義、條件編譯
    2022-09-09
  • C++實現(xiàn)LeetCode(50.求x的n次方)

    C++實現(xiàn)LeetCode(50.求x的n次方)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(50.求x的n次方),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 淺談關(guān)于C語言中#define的副作用

    淺談關(guān)于C語言中#define的副作用

    這篇文章主要介紹了關(guān)于C語言中#define的副作用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評論

平遥县| 巩留县| 宁明县| 宁蒗| 龙山县| 贵德县| 蓝田县| 江阴市| 保康县| 宾川县| 南汇区| 方山县| 宾川县| 巴中市| 昌黎县| 嘉祥县| 定南县| 和田市| 孝昌县| 临桂县| 宁远县| 龙井市| 宣化县| 加查县| 屏东市| 繁峙县| 垫江县| 曲麻莱县| 南乐县| 岳池县| 曲松县| 太康县| 荆州市| 观塘区| 林芝县| 兴业县| 应城市| 台北县| 蒲江县| 泰顺县| 千阳县|