Determining if a string is a double

前端 未结 5 696
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 01:38

I would like to see if a string contains a double as its sole contents. In other words, if it could possibly be the output of the following function:

string          


        
5条回答
  •  自闭症患者
    2020-12-19 02:26

    Or use streams directly:

    #include 
    #include 
    #include 
    
    template
    bool isValid(std::string const& num)
    {
        T  value;
        std::stringstream stream(num);
        stream >> value;
    
        // If the stream is already in the error state peak will not change it.
        // Otherwise stream should be good and there should be no more data
        // thus resulting in a peek returning an EOF
        return (stream) &&
               stream.peek() == std::char_traits::eof();
    }
    
    int main()
    {
        isValid("55");
    }
    

提交回复
热议问题