boost-string_algo字符串算法庫

1.lexical_cast:可以進行字符串,整數/浮點數之間的字面值轉換。

#include <boost/lexical_cast.hpp>//所在頭文件
int main()
{
    int x = lexical_cast<int>("100");  //string->int 
    double y = lexical_cast<double>("3.14159e3");  //string->double
 
    cout<<x<<endl;//100;
    cout<<y<<endl;//314.159;

    string str1 = lexical_cast<string>(100);//int -> string
    string str2 = lexical_cast<string>(3.14159);//double -> string
    cout<<str1<<str1[0]<<endl;//1001
    cout<<str2<<str2[0]<<endl;//3.14158999999999993

    bool flag = lexical_cast<bool>("1");//string -> bool
    cout<<flag<<endl;

    return 0;   
}
此外還有一個bad_lexical_cast類,用來捕獲lexical_cast過程中出現的異常。
try
{
	lexical_cast<int>("hello");
}
catch(bad_lexical_cast& e)
{
	cout<<e.what()<<endl;
}
2.format
#include <boost/format.hpp>支持格式化輸出。

3.string_algo庫,是一個針對string字符串的一個比較完備的算法庫。

位置:#include <boost/algorithm/string.hpp>

算法庫分類:大小寫轉換、判斷式與分類、修剪、查找和替換、分割和合並。

3.1大小寫轉換:
to_upper()//修改源字符串。轉換成大寫字符
to_upper_copy()//不改變源字符串,返回修改後的值。
to_lower()//轉換成小寫字符

3.2判斷式與分類:
starts_with()
ends_with()
contains()
equals()
lexicographical_compare() //根據字典順序檢測一個字符串是否小於另一個。
all() //檢測字符串中所有的字符是否滿足判斷式。
使用帶前綴 i 的算法可以實現大小寫無關的,判斷如:ilexicographical_compare

3.3函數對象判斷式:
is_equal()
is_less()
is_not_gteater()

代碼:

#include <boost/algorithm/string.hpp>
#include <cassert>//注意添加使用命名空間
int main()
{
	string str("I don't Know");
	//to_upper(str);//轉換成小寫;
	assert( iends_with(str, "know") );
	assert( ends_with(str, "Know") );
	assert( contains(str, "no") );
	assert( icontains(str, "NO") );
	assert( ilexicographical_compare("abc", str) );

	is_equal()(str, "abc");//is_equal是一個函數對象,
	//第一對括號用於創建一個對象。然後使用函數對象重載的()函數調用符。

	cout<<str<<endl;

	return 0;   
}
3.4分類:
is_space
is_alnum
is_alpha
is_cntrl
is_digit
is_graph
is_lower
is_print
is_punct  

3.5 修剪算法:
trim_letf, trim_right, trim //可以刪除字符串開頭和結尾的部分的空格
同時他們都有_if和_copy兩種後綴版本。trime_letf_copy_if(str, is_digit());

3.6查找算法:
find_first, find_last, find_nth, find_head, find_tail.
//返回值爲iterator_range<string::iterator> rge類型
如:iterator_range<string::iterator> rge;
rge = find_first(str, "long");

3.7 替換和刪除:
replace/erase_first;//替換或刪除在輸入中第一次出現的時候。
replace/erase_last;
replace/erase_nth;
replace/erase_head;
replace/erase_tail;
//前兩個算法都有前綴i,後綴_copy和組合,後四個只有後綴_copy的兩個版本。
string str = "long long ago, there was a king";
string str1 = replace_first_copy(str, "long", "small");

3.8 分割算法:
find_all;有忽略大小寫的前綴i的版本。
split;

3.9 合併算法:
join是分割算法的逆運算。

3.10 tokenizer:
是一個專門用於分詞的字符串處理庫。
#include <boost/tokenzier.hpp>
string str = "a b c d ,.e ";
tokenizer<> tok(str);//默認的分詞謂詞是空格和標點。

for(BOOST_AUTO(pos, tok.begin()); pos!=tok.end(); pos++)
cout<<*pos<<endl;
缺點:只支持使用單個字符進行分詞,不知道多個字符如".."

4 正則表達式庫:
xpressive:能夠解決文本的絕大數問題如:驗證、匹配、查找、替換。//雖然正則庫覆蓋了string_algo的所有算法,但是前者比較龐大,沒有後者效率高。
動態用法:#include <boost/xpressive/xpressive_dynamic.hpp>//動態庫是正則庫的主體部分。
using namespace boost::xpressive;
正則表達式:
. 是單字符的通配符。
^ 匹配行的開頭
$ 匹配行的末尾
* 表示可以重複任意多次(n>=0)
+ 表示可以重複一次或多次(n>0)
? 表示n=0或1
()定義一個正則表達式子元素,可以引用或重複。
{} 可以手工指定重複的次數,{n}重複x=n次。
[] 用於定義字符集合。
\ 轉義字符,特殊字符轉以後自身匹配。\.
\d相當於[0-9],\w=[a-z],\s 匹配空格。
| 或,匹配它兩的元素之一。

#include <boost/xpressive/xpressive_dynamic.hpp>

using namespace boost::xpressive;		

int main() 
{
    using namespace boost::xpressive;

    cregex reg = cregex::compile("a*c");//產生正則表達式對象。

    assert( regex_match("abc", reg) );//匹配成功
    assert( regex_match("abbc", reg) );//判斷字符,是否匹配reg的正則。
	
	cregex reg = cregex::compile("\\d{6}((1|2)\\d{3})((0|1)\\d)([0-3]\\d)(\\d{3}(X|\\d))"
        , icase);//產生正則表達式對象。 icase用於忽略大小寫。
    //子括號裏仍是表達式,共有七個表達式。
    cmatch what;
    assert( regex_match("32940119910903403X", what, reg) );//匹配成功
    
    for(BOOST_AUTO(pos, what.begin()); pos != what.end(); ++pos)
        cout<<*pos<<endl;
    cout<<"date:"<<what[1]<<what[3]<<what[5]<<endl;
	
	regex_replace(str, reg1, "manual");//將str中和reg1匹配的表達式替換爲"manual"	
	
    return 0;   
}
//如果同一行的str有多個內容匹配正則式。可以使用,(比較特殊的一種情況)
int main() 
{
    using namespace boost::xpressive;
    std::ifstream ifs;
    ifs.open("test.txt");
    //std::ifstream ifs("test.txt");
    string str = "there is a power-suit item";;
    sregex reg = sregex::compile("(power)-(.{4})", icase);
    sregex_iterator end;
    smatch what;
    while( getline(ifs, str) )
    {
        sregex_iterator pos(str.begin(), str.end(), reg);//迭代器模板。
        while( pos != end )
        {
            cout<<(*pos)[0]<<" ";
            ++pos;
        }   
        cout<<endl;
    }    
    return 0;   
}

5.boost容器

boost容器很多,而且在STL的基礎上添加了很多容器,但是使用範圍比較狹窄,所以我們只需要瞭解就可以。

1.array;
速度、性能上與原始數組相差無幾。只是包裝了普通的數組。屬於靜態數組,不支持動態分配。
2.dynamic_bitset//位數組
3.散列容器:unordered_set, unordered_map; //其實就是hash_map
4.bimap是雙向映射容器。分爲左右視圖。
5.circular_buffer :循環緩衝區的數據結構。
6.tuple(元組):有固定數目元素的容器,是std::pair的泛化。
7.any只能容納一個元素,但是這個元素可以是任意類型。
8.variant和any有些類似,是一種可變類型。variant是對union的擴展。
9.multi_array多維數組。
10.property_tree庫容器可以解析xml,json,ini,info的配置文件。

  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章