代码改变世界

boost学习笔记第一节

2013-06-27 14:57  xiaocaiju  阅读(79)  评论(0)    收藏  举报
lexical_cast
#include "boost/lexical_cast.hpp"
    
using  boost::lexical_cast;
     using  boost::bad_lexical_cast;
    string strNum =  "3.45" ;
    DOUBLE        nNum = 0;
     try
    {
        nNum = lexical_cast<DOUBLE>(strNum);
    }
     catch (bad_lexical_cast &e)
    {
        cout<<e.what();
    }


    DWORD dwError = GetLastError();
    string strError = lexical_cast<string>(dwError);
    MessageBoxA(NULL, strError.c_str(), NULL, MB_OK);
 
Format
1. printf风格
2. 参数个数确定
cout<<format("%1% %2% %1%\n" % 64 % "hello");
cout<<format("%s %d" % "tiger" % 12);
 
    format fmter( "%2% %1%" );
    fmter % 36; fmter % 37;
    string    str1 = fmter.str();
    string strFormat =  str(format( "%2% %1%" ) % 36 % 77);
 
algorithm
to_upper, to_lower_copy, ireplace_first_copy

#include   "boost/algorithm/string.hpp"
    string strHello( "hello, world" );
    to_upper(strHello);
    string     strGoodBye = to_lower_copy(ireplace_first_copy(strHello,  "hello" ,  "goodbye" ));
 
predicates
 

#include   "boost/algorithm/string.hpp"
#include   "boost/algorithm/string/predicate.hpp"

using   namespace  boost::algorithm;
BOOL is_executable(string& filename)
{
     return  (iends_with(filename,  ".exe" ) ||
        iends_with(filename,  ".com" ));
}

    string strComFile( "command.com" );
    string strPrint = strComFile + (is_executable(strComFile)?  " is "  :  " is not " )
        +  " execute file \n" ;
    string strNoExecFile( "hello" );
    strPrint = strNoExecFile + ( all(strNoExecFile, is_lower() )?  "is "  :  "is not" )
        +  "execute file \n" ;
 
Triming
trim_left_of, trim_right_copy, is_any_of, is_digit
    string str1= "     hello world!     " ;
    string str2=trim_left_copy(str1);    // str2 == "hello world!     "
    string str3=trim_right_copy(str1);   // str3 == "     hello world!"
    trim(str1);                          // str1 == "hello world!"
    string phone= "0042333snfd3444" ;
     // remove leading 0 from the phone number
    trim_left_if(phone,is_any_of( "0" ));  // phone == "423333444"
 
Find
iterator_range, transform, bind2nd, find_first

char text[]="hello dolly!";
    iterator_range<char*> result=find_last(text,"ll");
 
    transform( result.begin(), result.end(), result.begin(), bind2nd(plus<char>(), 1) );
    // text = "hello dommy!"           
 
    to_upper(result); // text == "hello doMMy!"
 
    // iterator_range is convertible to bool
    if(find_first(text, "dolly"))
    {
        cout << "Dolly is there" << endl;
    }
 
Replace
replace_first, replace_last, erase_all, erase_head
string str1="Hello  Dolly,   Hello World!"
    replace_first(str1, "Dolly", "Jane");      // str1 == "Hello  Jane,   Hello World!"
    replace_last(str1, "Hello", "Goodbye");    // str1 == "Hello  Jane,   Goodbye World!"
    erase_all(str1, " ");                      // str1 == "HelloJane,GoodbyeWorld!"
    erase_head(str1, 5);                       // str1 == "Jane,GoodbyeWorld!"
 
Find Iterator
 
Split
 
 
string str1("hello abc-*-ABC-*-aBc goodbye");
 
    typedef vector< iterator_range<string::iterator> > find_vector_type;
 
    find_vector_type FindVec; // #1: Search for separators
    ifind_all( FindVec, str1, "abc" ); // FindVec == { [abc],[ABC],[aBc] }
 
    typedef vector< string > split_vector_type;
 
    split_vector_type SplitVec; // #2: Search for tokens
    split( SplitVec, str1, is_any_of("-*"), token_compress_on ); // SplitVec == { "hello abc","ABC","aBc goodbye"