[BOOST]學習使用點滴-1-字元處理[by galphy]

來源:互聯網
上載者:User

[20050120]
 1.字元轉換
 1.1 相關類
   lexical_cast
   bad_lexical_cast
 1.2 定義檔案:#include <boost/lexical_cast.hpp>
 1.3 功能簡介:
   用來進行類型轉換的,字串與數值之間的轉換
 1.4 使用舉例:
  Eclipse 3.0, GUN G++ , Winxp測試通過。
  #include <iostream>
  #include <boost/lexical_cast.hpp>

  using namespace std;

  main()
  {
   int i=100;
   char* pstr = "10101";
 
   cout<<"Before convert i is:"<<i<<endl;
   i = boost::lexical_cast< int >(pstr);
   cout<<"After convert i is:"<<i<<endl;
 
   return 0;
  }
  //print: Before convert i is:100
  //     After convert i is:10101
 
 1.5 其他:
  轉換的異常處理和一般的異常處理相同。
  如:try
        {
            //進行轉換
        }
        catch(bad_lexical_cast &)
        {
            //處理異常
        }
   
 2.格式化字串
 2.1 相關類
   format
 2.2 定義檔案:#include <boost/format.hpp>
 2.3 功能簡介:
   構建一個格式化的字串,其中的某些字元是待定的,其可以以參數的形式寫入。
 2.4 使用舉例:
    Eclipse 3.0, GUN G++ , Winxp測試通過。
    #include <iostream>
    #include <boost/format.hpp>
    #include <string>

    using namespace std;
  
    using boost::format;
    using boost::io::group;
    int main()
    {
         format fmt("%1% %2% %3% %4% %5% %6%/n");
         //使用%1%進行參數替換,1代表第一個參數
         string str;
         int i=100;
         cout<<fmt  % 'g' % 'a' % 'l' % 'p' % 'h' % 'y'<<endl;
         //%操作符壓入參數
         string s_tmp =boost::str(fmt);
         //str()自由函數,fomat->string
         //同樣也可使用format.str();

         cout << s_tmp;
         return 0;
    }
 2.5 格式說明:
    “$“的使用:後面加printf的格式控制符
    “|“的使用:簡化printf的格式控制符
    1.無參數順序:
        format(“%5d %4s %5f“); //%後面直接加printf的格式控制符,參數順序傳入
        format(“%|5| %|4| %|5|);//效果同上,自動判斷d,s,f等控制符號
    2.有參數順序:
        format(“%1% %2% %1%“); 
        format(“%1$5d  %2$5s %1$5d);
        format(“%|1$5|  %|2$5| %|1$5|);//效果同上,自動判斷d,s,f等控制符號
    3.具體格式:

        [ N$ ] [ flags ] [ 寬度] [ . 精度] type-char
                    

  • Flag Meaning effect on internal stream
    '-' left alignment N/A (applied later on the string)
    '=' centered alignment N/A (applied later on the string)
    - note : added feature, not in printf -
    '_' internal alignment sets internal alignment
    - note : added feature, not in printf -
    '+' show sign even for positive numbers sets showpos
    '#' show numerical base, and decimal point sets showbase and showpoint
    '0' pad with 0's (inserted after sign or base indicator) if not left-aligned, calls setfill('0') and sets internal
    Extra actions are taken after stream conversion to handle user-defined output.
    ' ' if the string does not begin with + or -, insert a space before the converted string N/A (applied later on the string)
    Different to printf's behaviour : it is not affected by internal alignment
  • Type-Char Meaning effect on stream
    p or x hexadecimal output sets hex
    o octal output sets oct
    e scientific float format sets floatfield bits to scientific
    f fixed float format sets floatfield bits to fixed
    g general -default- float format unset all floatfield bits
    X, E or G same effect as their lowercase counterparts, but using uppercase letters for number outputs. (exponents, hex digits, ..) same effects as 'x', 'e', or 'g', plus uppercase
    d, i or u decimal type output sets basefield bits to dec
    s or S string output precision specification is unset, and its value goes to an internal field for later 'truncation'. (see precision explanation above)
    c or C 1-character output only the first character of the conversion string is used.
    % print the character % N/A

            

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.