Problem with re-using a stringstream object

前端 未结 4 1634
孤独总比滥情好
孤独总比滥情好 2021-02-08 18:43

I\'m trying to use safe practices in handling input with numbers only in C++, so I use a stringstream object as so:

#include 
#include 

        
4条回答
  •  终归单人心
    2021-02-08 19:02

    A better way to do this conversion between datatypes would be to use boost::lexical_cast.

    Info and examples can be found at the Boost Site.

    Below is an example of doing an int to string conversion and back (string to int) such as what you're doing in your program.

    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        int i = 42;
        std::string s = boost::lexical_cast(i);
        int j = boost::lexical_cast(s);
    
        return 1;
    }
    

提交回复
热议问题