• <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是強大的,但不是萬能的,但在很多情況下他有著獨特的優點,安全方便快捷!!!

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

            一本色道久久99一综合| 久久99精品国产麻豆不卡| 99久久精品国产一区二区| AV色综合久久天堂AV色综合在 | 久久国产精品一区| 伊人 久久 精品| 久久99国产精品久久99| 人妻中文久久久久| 久久久久成人精品无码中文字幕| 国产激情久久久久影院| 久久久久亚洲AV片无码下载蜜桃| 国产亚洲婷婷香蕉久久精品| 午夜精品久久久久久影视777 | 精品久久无码中文字幕| 久久久WWW免费人成精品| 无码人妻精品一区二区三区久久久| 久久99国产一区二区三区| 久久天天躁狠狠躁夜夜96流白浆| 天堂无码久久综合东京热| 狠狠色丁香久久综合五月| 国内精品伊人久久久久777| 久久精品国产亚洲7777| 久久se精品一区二区| 午夜精品久久久久久中宇| 99久久做夜夜爱天天做精品| 94久久国产乱子伦精品免费| 久久婷婷五月综合国产尤物app| 中文字幕亚洲综合久久菠萝蜜| 国产综合成人久久大片91| 国产精品久久波多野结衣| 久久精品国产精品亚洲毛片| 97精品依人久久久大香线蕉97| 亚洲AV伊人久久青青草原| 久久久久久毛片免费看| 久久精品国产欧美日韩| 久久久91人妻无码精品蜜桃HD| 久久成人国产精品二三区| 久久青青草原综合伊人| 人人狠狠综合久久亚洲88| 久久99国产精品成人欧美| 久久久精品视频免费观看|