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;
}