欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實(shí)例講述了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è)計(jì)有所幫助。

相關(guān)文章

  • C語言?const修飾普通變量和指針的操作代碼

    C語言?const修飾普通變量和指針的操作代碼

    這篇文章主要介紹了C語言const修飾普通變量和指針,用const修飾普通變量時,是在語法層面限制了變量的修改,但是本質(zhì)上,變量還是變量,是一種不能被修改的變量,本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • C語言實(shí)現(xiàn)貪吃蛇小游戲

    C語言實(shí)現(xiàn)貪吃蛇小游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C++使用函數(shù)的一些高級操作指南

    C++使用函數(shù)的一些高級操作指南

    C++中函數(shù)調(diào)用的方法與C語言并無區(qū)別,依舊是在調(diào)用方函數(shù)中執(zhí)行函數(shù)調(diào)用語句來實(shí)現(xiàn)函數(shù)調(diào)用,下面這篇文章主要給大家介紹了關(guān)于C++使用函數(shù)的一些高級操作,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • C語言中g(shù)etchar和putchar的使用方法詳解

    C語言中g(shù)etchar和putchar的使用方法詳解

    我們知道scanf函數(shù)可以從鍵盤輸入信息,而printf則可以輸出信息,同樣地,getchar和putchar也有同樣的功能,下面我來給大家介紹putchar和getchar的使用方法,需要的朋友可以參考下
    2023-08-08
  • floyd算法實(shí)現(xiàn)思路及實(shí)例代碼

    floyd算法實(shí)現(xiàn)思路及實(shí)例代碼

    這篇文章主要介紹了floyd算法實(shí)現(xiàn)思路及實(shí)例代碼,有需要的朋友可以參考一下
    2014-01-01
  • C++ 中this指針的用途詳解

    C++ 中this指針的用途詳解

    這篇文章主要給大家介紹了關(guān)于C++ 中this指針的用途,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-09-09
  • c++傳遞函數(shù)指針和bind的示例

    c++傳遞函數(shù)指針和bind的示例

    這篇文章主要介紹了c++傳遞函數(shù)指針和bind的示例,需要的朋友可以參考下
    2014-05-05
  • 利用C語言實(shí)現(xiàn)猜數(shù)字小游戲

    利用C語言實(shí)現(xiàn)猜數(shù)字小游戲

    這篇文章主要為大家詳細(xì)介紹了利用C語言實(shí)現(xiàn)猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • C++超詳細(xì)梳理IO流操作

    C++超詳細(xì)梳理IO流操作

    當(dāng)程序與外界進(jìn)行信息交換時,存在兩個對象,一個是程序中的對象,另一個是文件對象。流是信息流動的一種抽象,它負(fù)責(zé)在數(shù)據(jù)的生產(chǎn)者和數(shù)據(jù)的消費(fèi)者之間建立聯(lián)系,并管理數(shù)據(jù)的流動
    2022-07-07
  • C++多線程編程簡單實(shí)例

    C++多線程編程簡單實(shí)例

    本文給大家分享的是C++多線程編程簡單實(shí)例,由于C++本身沒有多線程機(jī)制,在windows下我們使用調(diào)用SDK win32 api來實(shí)現(xiàn),示例都很簡單,講解的也很詳細(xì),推薦給大家。
    2015-03-03

最新評論