How to convert a number to string and vice versa in C++

前端 未结 5 1083
鱼传尺愫
鱼传尺愫 2020-11-22 03:36

Since this question gets asked about every week, this FAQ might help a lot of users.

  • How to convert an integer to a string in C++

  • how to con

5条回答
  •  星月不相逢
    2020-11-22 04:19

    How to convert a number to a string in C++03

    1. Do not use the itoa or itof functions because they are non-standard and therefore not portable.
    2. Use string streams

       #include   //include this to use string streams
       #include  
      
      int main()
      {    
          int number = 1234;
      
          std::ostringstream ostr; //output string stream
          ostr << number; //use the string stream just like cout,
          //except the stream prints not to stdout but to a string.
      
          std::string theNumberString = ostr.str(); //the str() function of the stream 
          //returns the string.
      
          //now  theNumberString is "1234"  
      }
      

      Note that you can use string streams also to convert floating-point numbers to string, and also to format the string as you wish, just like with cout

      std::ostringstream ostr;
      float f = 1.2;
      int i = 3;
      ostr << f << " + " i << " = " << f + i;   
      std::string s = ostr.str();
      //now s is "1.2 + 3 = 4.2" 
      

      You can use stream manipulators, such as std::endl, std::hex and functions std::setw(), std::setprecision() etc. with string streams in exactly the same manner as with cout

      Do not confuse std::ostringstream with std::ostrstream. The latter is deprecated

    3. Use boost lexical cast. If you are not familiar with boost, it is a good idea to start with a small library like this lexical_cast. To download and install boost and its documentation go here. Although boost isn't in C++ standard many libraries of boost get standardized eventually and boost is widely considered of the best C++ libraries.

      Lexical cast uses streams underneath, so basically this option is the same as the previous one, just less verbose.

      #include 
      #include 
      
      int main()
      {
         float f = 1.2;
         int i = 42;
         std::string sf = boost::lexical_cast(f); //sf is "1.2"
         std::string si = boost::lexical_cast(i); //sf is "42"
      }
      

    How to convert a string to a number in C++03

    1. The most lightweight option, inherited from C, is the functions atoi (for integers (alphabetical to integer)) and atof (for floating-point values (alphabetical to float)). These functions take a C-style string as an argument (const char *) and therefore their usage may be considered a not exactly good C++ practice. cplusplus.com has easy-to-understand documentation on both atoi and atof including how they behave in case of bad input. However the link contains an error in that according to the standard if the input number is too large to fit in the target type, the behavior is undefined.

      #include  //the standard C library header
      #include 
      int main()
      {
          std::string si = "12";
          std::string sf = "1.2";
          int i = atoi(si.c_str()); //the c_str() function "converts" 
          double f = atof(sf.c_str()); //std::string to const char*
      }
      
    2. Use string streams (this time input string stream, istringstream). Again, istringstream is used just like cin. Again, do not confuse istringstream with istrstream. The latter is deprecated.

      #include 
      #include 
      int main()
      {
         std::string inputString = "1234 12.3 44";
         std::istringstream istr(inputString);
         int i1, i2;
         float f;
         istr >> i1 >> f >> i2;
         //i1 is 1234, f is 12.3, i2 is 44  
      }
      
    3. Use boost lexical cast.

      #include 
      #include 
      
      int main()
      {
         std::string sf = "42.2"; 
         std::string si = "42";
         float f = boost::lexical_cast(sf); //f is 42.2
         int i = boost::lexical_cast(si);  //i is 42
      }       
      

      In case of a bad input, lexical_cast throws an exception of type boost::bad_lexical_cast

提交回复
热议问题