【轉載】boost::lexical_cast 的使用

來源:互聯網
上載者:User

1,字串 到 數實值型別的轉換

2,數值 到 字串的轉換
3,異常處理情況
4,boost::lexical_cast 的原型:
template<typename Target, typename Source>
    Target lexical_cast(Source arg);
lexical_cast 是依賴於字串流 std::stringstream 的,其原理也是相當的簡單:把源類型 (Source) 讀入到字元流中,再寫到目標類型 (Target) 中。但這裡同時也帶來了一些限制:
  - 輸入資料 (arg) 必須能夠 “完整” 地轉換,否則就會拋出 bad_lexical_cast 異常。例如:
   int i = boost::lexical_cast<int>("123.456"); // this will throw
    因為 “123.456” 只能 “部分” 地轉換為 123,不能 “完整” 地轉換為 123.456,還是讓我們需要適當的注意一些這兩個模板參數就好了。
  - 由於 Visual C++ 6 的本地化(locale)部分實現有問題,如果使用了非預設的 locale,可能會莫名其妙地拋出異常。
  - 源類型 (Source) 必須是一個可以輸出到輸出資料流的類型(OutputStreamable),也意味著該類型需要 operator<< 被定義。
  - 同樣的,目標類型 (Target) 必須是一個可以輸入到輸入資料流的類型 (InputStreamable),也意味著該類型需要 operator>> 被定義。
  - 另外,Both Source and Target are CopyConstructible。
  - Target is DefaultConstructible。
其中,下面的四個限制是在使用自訂類型之間轉換時必須做的一些工作的(當然流行的使用的 C 庫函數等等都是不可以處理自訂類型之間的轉換的)。

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string> 
#define ERROR_LEXICAL_CAST     1 
int main()
{
    using boost::lexical_cast;
    int         a = 0;
    double        b = 0.0;
    std::string s = ""; 
    int            e = 0;    
    try
    { 
        // ----- 字串 --> 數值 
        a = lexical_cast<int>("123");
        b = lexical_cast<double>("123.12");
        // ----- 數值 --> 字串
        s = lexical_cast<std::string>("123456.7"); 
        // ----- 異常處理示範
        e = lexical_cast<int>("abc");
    }
    catch(boost::bad_lexical_cast& e)
    {
        // bad lexical cast: source type value could not be interpreted as target
        std::cout << e.what() << std::endl;
        return ERROR_LEXICAL_CAST;
    } 
    
    std::cout << a << std::endl;    // 輸出:123 
    std::cout << b << std::endl;    // 輸出:123.12 
    std::cout << s << std::endl;     // 輸出:123456.7 
    return 0;
}

1,字串 到 數實值型別的轉換

2,數值 到 字串的轉換
3,異常處理情況
4,boost::lexical_cast 的原型:
template<typename Target, typename Source>
    Target lexical_cast(Source arg);
lexical_cast 是依賴於字串流 std::stringstream 的,其原理也是相當的簡單:把源類型 (Source) 讀入到字元流中,再寫到目標類型 (Target) 中。但這裡同時也帶來了一些限制:
  - 輸入資料 (arg) 必須能夠 “完整” 地轉換,否則就會拋出 bad_lexical_cast 異常。例如:
   int i = boost::lexical_cast<int>("123.456"); // this will throw
    因為 “123.456” 只能 “部分” 地轉換為 123,不能 “完整” 地轉換為 123.456,還是讓我們需要適當的注意一些這兩個模板參數就好了。
  - 由於 Visual C++ 6 的本地化(locale)部分實現有問題,如果使用了非預設的 locale,可能會莫名其妙地拋出異常。
  - 源類型 (Source) 必須是一個可以輸出到輸出資料流的類型(OutputStreamable),也意味著該類型需要 operator<< 被定義。
  - 同樣的,目標類型 (Target) 必須是一個可以輸入到輸入資料流的類型 (InputStreamable),也意味著該類型需要 operator>> 被定義。
  - 另外,Both Source and Target are CopyConstructible。
  - Target is DefaultConstructible。
其中,下面的四個限制是在使用自訂類型之間轉換時必須做的一些工作的(當然流行的使用的 C 庫函數等等都是不可以處理自訂類型之間的轉換的)。

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string> 
#define ERROR_LEXICAL_CAST     1 
int main()
{
    using boost::lexical_cast;
    int         a = 0;
    double        b = 0.0;
    std::string s = ""; 
    int            e = 0;    
    try
    { 
        // ----- 字串 --> 數值 
        a = lexical_cast<int>("123");
        b = lexical_cast<double>("123.12");
        // ----- 數值 --> 字串
        s = lexical_cast<std::string>("123456.7"); 
        // ----- 異常處理示範
        e = lexical_cast<int>("abc");
    }
    catch(boost::bad_lexical_cast& e)
    {
        // bad lexical cast: source type value could not be interpreted as target
        std::cout << e.what() << std::endl;
        return ERROR_LEXICAL_CAST;
    } 
    
    std::cout << a << std::endl;    // 輸出:123 
    std::cout << b << std::endl;    // 輸出:123.12 
    std::cout << s << std::endl;     // 輸出:123456.7 
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.