boost/lexical_cast.hpp的簡單使用方法

來源:互聯網
上載者:User

1、字串->數值 

C++代碼

  1. #include <boost/lexical_cast.hpp>   
  2. #include <iostream>   
  3. int main()   
  4. {   
  5.         using boost::lexical_cast;   
  6.         int a = lexical_cast<int>("123");   
  7.         double b = lexical_cast<double>("123.12");   
  8.          std::cout<<a<<std::endl   
  9.          std::cout<<b<<std::endl;   
  10.         return 0;   
  11. }  

#include <boost/lexical_cast.hpp>#include <iostream>int main(){ using boost::lexical_cast; int a = lexical_cast<int>("123"); double b = lexical_cast<double>("123.12");
std::cout<<a<<std::endl std::cout<<b<<std::endl; return 0;}


2、數值->字串 

C++代碼

  1. #include <boost/lexical_cast.hpp>   
  2. #include <string>   
  3. #include <iostream>   
  4. int main()   
  5. {   
  6.         using std::string;   
  7.         const double d = 123.12;   
  8.          string s = boost::lexical_cast<string>(d);   
  9.          std::cout<<s<<std::endl;   
  10.         return 0;   
  11. }  

#include <boost/lexical_cast.hpp>#include <string>#include <iostream>int main(){ using std::string; const double d = 123.12; string s = boost::lexical_cast<string>(d);
std::cout<<s<<std::endl; return 0;}

3、異常 

C++代碼

  1. #include <boost/lexical_cast.hpp>   
  2. #include <iostream>   
  3. int main()   
  4. {   
  5.         using std::cout;   
  6.         using std::endl;   
  7.         int i;   
  8.         try  
  9.          {   
  10.              i = boost::lexical_cast<int>("xyz");   
  11.          }   
  12.         catch(boost::bad_lexical_cast& e)   
  13.          {   
  14.              cout<<e.what()<<endl;   
  15.             return 1;   
  16.          }   
  17.          cout<<i<<endl;   
  18.         return 0;   
  19. }  

#include <boost/lexical_cast.hpp>#include <iostream>int main(){ using std::cout; using std::endl; int i; try { i = boost::lexical_cast<int>("xyz");
} catch(boost::bad_lexical_cast& e) { cout<<e.what()<<endl; return 1; } cout<<i<<endl; return 0;}

顯然“xyz”並不能轉換為一個int類型的數值,於是拋出異常,捕捉後輸出“bad lexical cast: source type value could not be interpreted as target”這樣的資訊。 

4、注意事項 

lexical_cast依賴於字元流std::stringstream,其原理相當簡單:把源類型讀入到字元流中,再寫到目標類型中,就大功告成。 

C++代碼

  1. int d = boost::lexical_cast<int>("123");  

int d = boost::lexical_cast<int>("123");

相當於 

C++代碼

  1. int d;   
  2. std::stringstream s;   
  3. s<<"123";   
  4. s>>d;  

int d;std::stringstream s;s<<"123";s>>d;

5、小結 

我們已經體驗了boost::lexcial_cast。當然,lexical_cast不僅僅局限於字串類型與數實值型別之間的轉換:可在任意可輸出到stringstream的類型和任意可從stringstream輸入的類型間轉換。

聯繫我們

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