




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、boost的字符串算法 收藏 boost:algorithm簡介boost:algorithm提供了很多字符串算法,包括: 大小寫轉(zhuǎn)換; 去除無效字符; 謂詞; 查找; 刪除/替換; 切割; 連接; 我們用寫例子的方式來了解boost:algorithm能夠為我們做些什么。 boost:algorithm學(xué)習(xí)#include <boost/algorithm/string.hpp>using namespace std;using namespace boost; 一:大小寫轉(zhuǎn)換1 to_upper() 將字符串轉(zhuǎn)為大寫Example:string str1(" hell
2、o world! ");to_upper(str1); / str1 = " HELLO WORLD! "2 to_upper_copy() 將字符串轉(zhuǎn)為大寫,并且賦值給另一個字符串Example:string str1(" hello world! ");string str2;str2 = to_upper_copy(str1); / str2 = " HELLO WORLD! "3 to_lower() 將字符串轉(zhuǎn)為小寫Example:參看to_upper()4 to_lower_copy() 將字符串轉(zhuǎn)為小寫,并且賦
3、值給另一個字符串Example:參看to_upper_copy()二:Trimming(整理,去掉首尾的空格字符)1 trim_left() 將字符串開頭的空格去掉Example:string str1(" hello world! ");trim_left(str1); / str1 = "hello world! "2 trim_left_if() 將字符串開頭的符合我們提供的“謂詞”的特定字符去掉Example:bool NotH(const char &ch) if(ch = ' ' | ch = 'H'
4、| ch = 'h') return true; else return false;.string str1(" hello world! ");trim_left_if(str1, NotH); / str1 = "ello world! "3 trim_left_copy() 將字符串開頭的空格去掉,并且賦值給另一個字符串Example:string str1(" hello world! ");string str2;str2 = trim_left_copy(str1); / str2 = "hel
5、lo world! "4 trim_left_copy_if() 將字符串開頭的符合我們提供的“謂詞”的特定字符去掉,并且賦值給另一個字符串Example:string str1(" hello world! ");string str2;str2 = trim_left_copy_if(str1, NotH); / str2 = "ello world! "/ 將字符串結(jié)尾的空格去掉,示例請參看上面5 trim_right_copy_if()6 trim_right_if()7 trim_right_copy()8 trim_right()/
6、 將字符串開頭以及結(jié)尾的空格去掉,示例請參看上面9 trim_copy_if()10 trim_if()11 trim_copy()12 trim()三:謂詞1 starts_with() 判斷一個字符串是否是另外一個字符串的開始串Example:string str1("hello world!");string str2("hello");bool result = starts_with(str1, str2); / result = true2 istarts_with() 判斷一個字符串是否是另外一個字符串的開始串(不區(qū)分大小寫)Example
7、:string str1("hello world!");string str2("Hello");bool result = istarts_with(str1, str2); / result = true3 ends_with() 判斷一個字符串是否是另外一個字符串的結(jié)尾串4 iends_with() 判斷一個字符串是否是另外一個字符串的結(jié)尾串(不區(qū)分大小寫)5 contains() 判斷一個字符串是否包含另外一個字符串Example:string str1("hello world!");string str2("l
8、lo");bool result = contains(str1, str2); / result = true6 icontains() 判斷一個字符串是否包含另外一個字符串(不區(qū)分大小寫)7 equals() 判斷兩個字符串是否相等8 iequals() 判斷兩個字符串是否相等(不區(qū)分大小寫)9 lexicographical_compare() 按照字典排序,如果第一個字符串小于第二個字符串,返回true (我的boost1.33沒有實現(xiàn)?)10 ilexicographical_compare() 按照字典排序,如果第一個字符串小于第二個字符串,返回true(不區(qū)分大小寫)(
9、我的boost1.33沒有實現(xiàn)?)11 all() 判斷字符串中的所有字符是否全部滿足這個謂詞Example:bool is_123digit(const char &ch) if(ch = '1' | ch = '2' | ch = '3') return true; else return false;.string str1("12332211");bool result = all(str1, is_123digit); / result = truestr1 = "412332211"re
10、sult = all(str1, is_123digit); / result = false四:查找1 find_first() 從頭查找字符串中的子字符串,返回這個子串在原串中的iterator_range迭代器Example:char ToUpper(char &ch) if(ch <= 'z' && ch >= 'a') return ch + 'A'-'a' else return ch;.string str1("hello dolly!");iterator_r
11、ange<string:iterator> result = find_first(str1,"ll");transform( result.begin(), result.end(), result.begin(), ToUpper ); / str1 = "heLLo dolly!"2 ifind_first() 從頭查找字符串中的子字符串,返回這個子串在原串中的iterator_range迭代器(不區(qū)分大小寫)3 find_last() 從尾查找字符串中的子字符串,返回這個子串在原串中的iterator_range迭代器4 ifind_
12、last() 從尾查找字符串中的子字符串,返回這個子串在原串中的iterator_range迭代器(不區(qū)分大小寫)5 find_nth() 找到第n個匹配的子串(計算從0開始)Example:string str1("hello dolly!");iterator_range<string:iterator> result = find_nth(str1,"ll", 1);transform( result.begin(), result.end(), result.begin(), ToUpper ); / str1 = "hel
13、lo doLLy!"6 ifind_nth() 找到第n個匹配的子串(計算從0開始)(不區(qū)分大小寫)7 find_head() 找到字符串的前n個字節(jié)Example:string str1("hello dolly!");iterator_range<string:iterator> result = find_head(str1,5);transform( result.begin(), result.end(), result.begin(), ToUpper ); / str1 = "HELLO dolly!"8 find_t
14、ail() 找到字符串的后n個字節(jié)9 find_token() 找到符合謂詞的串Example:char Add1(const char &ch) return ch+1;.string str1("hello 1 world!");iterator_range<string:iterator> result = find_token(str1,is_123digit);transform( result.begin(), result.end(), result.begin(), Add1 ); / str1 = "hello 2 world
15、!");10 find_regex() 匹配正則表達(dá)式Example:(等稍候了解了boost的正則表達(dá)式后再給出)11 find() 使用自己寫的查找函數(shù)Example:iterator_range<string:iterator>MyFinder1( std:string:iterator begin, std:string:iterator end ) std:string:iterator itr; for(itr = begin;itr!=end;itr+) if(*itr) = '1') std:string:iterator preitr =
16、 itr; iterator_range<string:iterator> ret(preitr, +itr); return ret; return iterator_range<string:iterator>(); / boost自己也提供了很多Finder.string str1("hello 1 world!");iterator_range<string:iterator> result = find(str1,MyFinder1);transform( result.begin(), result.end(), result
17、.begin(), Add1 ); / str1 = "hello 2 world!");五:刪除/替換1 replace_first() 從頭找到第一個匹配的字符串,將其替換為給定的另外一個字符串Example:string str1("hello world!");replace_first(str1, "hello", "Hello"); / str1 = "Hello world!"2 replace_first_copy() 從頭找到第一個匹配的字符串,將其替換為給定的另外一個字符串,
18、并且賦值給另一個字符串Example:string str1("hello world!");string str2;str2 = replace_first_copy(str1, "hello", "Hello"); / str2 = "Hello world!"3 ireplace_first() 從頭找到第一個匹配的字符串,將其替換為給定的另外一個字符串(不區(qū)分大小寫)4 ireplace_first_copy() 從頭找到第一個匹配的字符串,將其替換為給定的另外一個字符串,并且賦值給另一個字符串(不區(qū)分大小
19、寫)5 erase_first() 從頭找到第一個匹配的字符串,將其刪除Example:string str1("hello world!");erase_first(str1, "llo"); / str1 = "He world!"6 erase_first_copy() 從頭找到第一個匹配的字符串,將其刪除,并且賦值給另一個字符串Example:string str1("hello world!");string str2;str2 = erase_first_copy(str1, "llo&quo
20、t;); / str2 = "He world!"7 ierase_first() 從頭找到第一個匹配的字符串,將其刪除(不區(qū)分大小寫)8 ierase_first_copy() 從頭找到第一個匹配的字符串,將其刪除,并且賦值給另一個字符串(不區(qū)分大小寫)/ 與上面類似,不過是從字符串尾開始替換9 replace_last()10 replace_last_copy()11 ireplace_last()12 ireplace_last_copy()13 erase_last()14 erase_last_copy()15 ierase_last()16 ierase_la
21、st_copy()/ 與上面類似,不過是從字符串第n個匹配的開始替換17 replace_nth() Example:string str1("hello world!");replace_nth(str1, "o", 1, "O"); / str1 = "hello wOrld!"18 replace_nth_copy()19 ireplace_nth()20 ireplace_nth_copy()21 erase_nth()22 erase_nth_copy()23 ierase_nth()24 ierase_
22、nth_copy()/ 與上面類似,不過是將所有的匹配字符串替換25 replace_all()26 replace_all_copy()27 ireplace_all()28 ireplace_all_copy()29 erase_all()30 erase_all_copy()31 ierase_all()32 ierase_all_copy()33 replace_head() 替換前n個字符Example:string str1("hello world!");replace_head(str1, 5, "HELLO"); / str1 = &q
23、uot;HELLO world!"34 replace_head_copy() 替換前n個字符,并且賦值給另一個字符串Example:string str1("hello world!");string str2;str2 = replace_head_copy(str1, 5, "HELLO"); / str2 = "HELLO world!"35 erase_head() 刪除前n個字符Example:string str1("hello world!");erase_head(str1, 5); /
24、 str1 = " world!"36 erase_head_copy() 刪除前n個字符,并且賦值給另一個字符串Example: string str1("hello world!");string str2;str2 = erase_head_copy(str1, 5); / str2 = " world!"/ 與上面類似(替換/刪除后n個字符)37 replace_tail() 38 replace_tail_copy() 39 erase_tail() 40 erase_tail_copy()/ 與正則表示式相關(guān),稍后了解。4
25、1 replace_regex() 42 replace_regex_copy() 43 erase_regex() 44 erase_regex_copy() 45 replace_all_regex() 46 replace_all_regex_copy() 47 erase_all_regex() 48 erase_all_regex_copy() / 不是很清楚,稍后了解49 find_format() 50 find_format_copy() 51 find_format_all() 52 find_format_all_copy()六:切割1 find_all() 查找所有匹配的
26、值,并且將這些值放到給定的容器中Example:string str1("hello world!");std:vector<std:string> result;find_all(result, str1, "l"); / result = 3("l","l","l")2 ifind_all() 查找所有匹配的值,并且將這些值放到給定的容器中(不區(qū)分大小寫)3 find_all_regex() 與正則表達(dá)式相關(guān),稍后了解4 split() 按照給定的謂詞切割字符串,并且把切割后的值放入到給定的容器中Example:class SplitNotThisCharpublic: SplitNotThisChar(const char ch) : m_char(ch) b
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)業(yè)種植確權(quán)管理辦法
- 高層建筑火災(zāi)模擬與人員安全疏散策略研究
- 教育的進(jìn)階之路:學(xué)校改進(jìn)策略與實踐探索
- 普通設(shè)備租賃管理辦法
- 液氯企業(yè)安全風(fēng)險隱患排查表
- 景區(qū)物業(yè)收費管理辦法
- 電氣工程雙創(chuàng)人才培養(yǎng)模式探討與實踐
- 重點高校自學(xué)考試課程體系優(yōu)化研究
- 幕墻工程工作總結(jié)
- 高校數(shù)字化資源服務(wù)系統(tǒng)用戶體驗優(yōu)化
- GB/T 8312-2002茶咖啡堿測定
- 2023年蘇州國發(fā)創(chuàng)業(yè)投資控股有限公司招聘筆試題庫及答案解析
- 通信線路工程施工組織設(shè)計方案【實用文檔】doc
- 護(hù)士注冊健康體檢表下載【可直接打印版本】
- 預(yù)計財務(wù)報表編制及分析課件
- 學(xué)生集體外出活動備案表
- Q∕SY 1347-2010 石油化工蒸汽透平式壓縮機(jī)組節(jié)能監(jiān)測方法
- 西門子順序功能圖語言S7-Graph的應(yīng)用
- 中醫(yī)治療室工作制度管理辦法
- 提花裝造工藝技術(shù)培訓(xùn)課程
- 直播傳媒公司簡介PPT課件(參考)
評論
0/150
提交評論