• <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 閱讀(821) 評論(0)  編輯 收藏 引用 所屬分類: GCC/G++跨平臺開發

            99久久www免费人成精品| 中文精品久久久久人妻不卡| 亚洲色大成网站www久久九| 久久久久久精品成人免费图片| 日韩AV毛片精品久久久| 久久影院综合精品| 久久99精品国产麻豆不卡| 久久精品国产亚洲AV香蕉| 久久本道伊人久久| 久久精品一本到99热免费| 国产精品18久久久久久vr| 久久美女网站免费| 热久久国产欧美一区二区精品| 久久综合狠狠综合久久| 亚洲国产一成久久精品国产成人综合| 国内精品人妻无码久久久影院导航| 久久精品国产亚洲AV大全| 思思久久99热只有频精品66| 91精品国产91久久综合| 中文字幕无码精品亚洲资源网久久| 品成人欧美大片久久国产欧美...| 性欧美丰满熟妇XXXX性久久久| 精品乱码久久久久久夜夜嗨| 国产91色综合久久免费分享| 2021国内精品久久久久久影院| 久久九九久精品国产免费直播| 91精品国产综合久久久久久| 久久久亚洲欧洲日产国码aⅴ | 亚洲综合日韩久久成人AV| 国产精久久一区二区三区| 青青草原综合久久| 2021少妇久久久久久久久久| 久久99精品久久久久久久不卡| 久久久久亚洲AV片无码下载蜜桃 | 久久精品一区二区国产| 97r久久精品国产99国产精| A狠狠久久蜜臀婷色中文网| 亚洲精品美女久久久久99| 少妇人妻88久久中文字幕| 国产精品无码久久综合| 久久九九青青国产精品|