std::lexical_cast - is there such a thing?

前端 未结 5 774
南方客
南方客 2020-12-01 02:30

Does the C++ Standard Library define this function, or do I have to resort to Boost?

I searched the web and couldn\'t find anything except Boost, but I thought I\'d

5条回答
  •  盖世英雄少女心
    2020-12-01 02:58

    There's no std::lexical_cast, but you can always do something similar with stringstreams:

    template 
    T lexical_cast(const std::string& str)
    {
        T var;
        std::istringstream iss;
        iss.str(str);
        iss >> var;
        // deal with any error bits that may have been set on the stream
        return var;
    }
    

提交回复
热议问题