• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

            路漫漫,長修遠,我們不能沒有錢
            隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
            數據加載中……

            boost的lexical_cast --數據類型轉

            注:轉載請保證文章完整性

            一、介紹

            lexical_cast是boost中一個非常有用,常用,好用的庫,我現在的小數據轉換用的都是lexical_cast。 lexical_cast最大的特點是安全,包括長度安全,類型安全。 下面我來介紹下lexical_cast的基本使用方法。

            Target lexical_cast(Source arg)
            

            例如:

            #include <boost/lexical_cast.hpp>           //lexical_cast的頭文件
            
            using namespace std;                        //stl的域
            using namespace boost;                      //boost的域
            
            int main() {
                const double PI = 3.1415926535;
                string str;
                str = lexical_cast<string>(PI);
                cout << str; 
                return 0;
            }
            

            非常容易吧,簡單也是他的優勢之一。

            二、異常

            lexical_cast在執行失敗時會拋出異常bad_lexical_cast 上面的例子改為:

            #include <boost/lexical_cast.hpp>           //lexical_cast的頭文件
            
            using namespace std;                        //stl的域
            using namespace boost;                      //boost的域
            
            int main() {
                try {
                    string str = "3.1415926535";
                    double PI = lexical_cast<double>(str);
                    cout << PI; 
                    return 0;
                } catch( bad_lexical_cast& E ) {
                    cout << E.what() << endl;
                }
            }
            

            當str為ABCD時, 無法轉成PI拋出異常 輸出

            bad lexical cast: source type value could not be interpreted as target
            

            三、一個小例子

            為了加深大加理解 下面使用lexical_cast實現一個簡單的文本輸入是否為指定類型的小程序

            #include <boost/lexical_cast.hpp>
            #include <iostream>
            
            using namespace std;                       //stl的域
            using namespace boost;                     //boost的域
            
            template<typename _T, typename _R>
            bool isElement(_R r) {
                try {
                    lexical_cast<_T>(r);                
                    return true;                        //轉換成功
                } catch(...) {
                    return false;
                }
            }
            
            int main() {
                try {
                    if( isElement<double>("3.14d159") )
                        cout << "YES" << endl;
                    else
                        cout << "NO" << endl;
            
                } catch( bad_lexical_cast& E ) {
                    cout << E.what() << endl;
                }
                return 0;    
            }
            

            測試結果:

            測試:isElement<double>("3.14d159") 輸出:NO
            測試:isElement<string>("3.14d159") 輸出:YES
            測試:isElement<long>("314159")     輸出:YES
            測試:isElement<long>("31.4159")    輸出:NO
            測試:isElement<char>("314159")     輸出:NO
            

            四、源碼分析

            #ifdef BOOST_NO_STRINGSTREAM // 我們知道stringstream和strstream分別
                                         // 是string和char*結構, lexical_cast考慮很全面的。
                #include <strstream>
            #else
                #include <sstream>
            #endif
            
            // bad_lexical_cast是bad_cast的繼承,所以很標準,支持和擴充性都很好。
            class bad_lexical_cast : public std::bad_cast {
                public:
                    bad_lexical_cast() :
                        source( &typeid(void) ), target( &typeid(void) )
                    {
                    }
            
                    bad_lexical_cast( const std::type_info &s, const std::type_info &t ) :
                        source(&s), target(&t)
                    {
                    }
            
                    //提供了兩個返回type_info的函數,為我們跟蹤調試類形轉換起到很好的做用。
                    const std::type_info &source_type() const 
                    {
                        return *source;
                    }
            
                    const std::type_info &target_type() const
                    {
                        return *target;
                    }
            
                    virtual const char *what() const throw()
                    {
                        return "bad lexical cast: "
                               "source type value could not be interpreted as target";
                    }
            
                    virtual ~bad_lexical_cast() throw()
                    {
                    }
                private:
                    const std::type_info *source;
                    const std::type_info *target;
                };
            

            type_info的具體用法是:E.source_type().name()就可能到類型名。

            核心轉換部分,用的是留的概念,從流內數據的剩余情況與流轉換成功與否情況來判斷操作是否成功。在不加域時這里就像一個黑盒子。

                    bool operator<<(const Source &input)
                    {
                        return !(stream << input).fail();
                    }
            
                    template<typename InputStreamable>
                    bool operator>>(InputStreamable &output)
                    {
                        return !is_pointer<InputStreamable>::value &&
                                stream >> output &&
                                (stream >> std::ws).eof();
                    }
            
                    bool operator>>(std::string &output)
                    {
                        #if defined(BOOST_NO_STRINGSTREAM)
                        stream << '\0';
                        #endif
                        output = stream.str();
                        return true;
                    }
            

            僅提供該入口,具體實現被封在detail域里面。

                template<typename Target, typename Source>
                Target lexical_cast(Source arg)
                {
                    detail::lexical_stream<Target, Source> interpreter;
                    Target result;
            
                    if(!(interpreter << arg && interpreter >> result))
                        throw_exception(bad_lexical_cast(typeid(Target), typeid(Source))); //拋出異常錯誤,
                    return result;
                }
            

            最后, 我們可以發現

            bad_lexical_cast(typeid(Target), typeid(Source)
            

            與上面

            bad_lexical_cast(const std::type_info &s, const std::type_info &t) 
                    :source(&s), target(&t)
            

            之間的區別,在我看來是寫倒了,不過不影響,也算是個不算bug的bug

            五、總結

            lexical_cast是強大的,但不是萬能的,但在很多情況下他有著獨特的優點,安全方便快捷?。。?/p>

            posted on 2017-10-14 16:26 Khan 閱讀(809) 評論(0)  編輯 收藏 引用 所屬分類: GCC/G++ 、跨平臺開發

            国产精品热久久毛片| 精品久久人人爽天天玩人人妻| 久久精品无码一区二区无码| 国产精品9999久久久久| 精品久久一区二区| 久久影视国产亚洲| 日韩人妻无码一区二区三区久久| 丰满少妇人妻久久久久久| 久久精品国产只有精品66| 久久久久99这里有精品10 | 中文成人久久久久影院免费观看| 无码任你躁久久久久久老妇| 久久久国产精品亚洲一区| 国产免费福利体检区久久 | 91久久婷婷国产综合精品青草| 久久精品国产一区二区电影| 午夜不卡久久精品无码免费 | 婷婷国产天堂久久综合五月| 青青草原精品99久久精品66 | 久久99热精品| 久久99九九国产免费看小说| 热99re久久国超精品首页| 久久亚洲精品成人无码网站| 国产精品美女久久久免费| 久久久噜噜噜www成人网| 亚洲欧美久久久久9999| 国产激情久久久久影院小草| 精品久久久久香蕉网| 99久久国产亚洲综合精品| 欧美麻豆久久久久久中文| 女人香蕉久久**毛片精品| 精品久久久久中文字幕日本| 久久久久久精品久久久久| 久久久久亚洲精品男人的天堂| 麻豆av久久av盛宴av| 久久香综合精品久久伊人| 老司机国内精品久久久久| 久久精品午夜一区二区福利| 久久久久久免费视频| 久久影视综合亚洲| 欧美精品丝袜久久久中文字幕|