Easiest way to convert int to string in C++

后端 未结 28 1785
甜味超标
甜味超标 2020-11-21 06:42

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

(1)



        
相关标签:
28条回答
  • 2020-11-21 07:10

    If you're using MFC, you can use CString:

    int a = 10;
    CString strA;
    strA.Format("%d", a);
    
    0 讨论(0)
  • 2020-11-21 07:11

    Picking up a discussion with @v.oddou a couple of years later, C++17 has finally delivered a way to do the originally macro-based type-agnostic solution (preserved below) without going through macro uglyness.

    // variadic template
    template < typename... Args >
    std::string sstr( Args &&... args )
    {
        std::ostringstream sstr;
        // fold expression
        ( sstr << std::dec << ... << args );
        return sstr.str();
    }
    

    Usage:

    int i = 42;
    std::string s = sstr( "i is: ", i );
    puts( sstr( i ).c_str() );
    
    Foo x( 42 );
    throw std::runtime_error( sstr( "Foo is '", x, "', i is ", i ) );
    

    Original answer:

    Since "converting ... to string" is a recurring problem, I always define the SSTR() macro in a central header of my C++ sources:

    #include <sstream>
    
    #define SSTR( x ) static_cast< std::ostringstream & >( \
            ( std::ostringstream() << std::dec << x ) ).str()
    

    Usage is as easy as could be:

    int i = 42;
    std::string s = SSTR( "i is: " << i );
    puts( SSTR( i ).c_str() );
    
    Foo x( 42 );
    throw std::runtime_error( SSTR( "Foo is '" << x << "', i is " << i ) );
    

    The above is C++98 compatible (if you cannot use C++11 std::to_string), and does not need any third-party includes (if you cannot use Boost lexical_cast<>); both these other solutions have a better performance though.

    0 讨论(0)
  • 2020-11-21 07:11

    C++11 introduced std::to_string() for numeric types:

    int n = 123; // Input, signed/unsigned short/int/long/long long/float/double
    std::string str = std::to_string(n); // Output, std::string
    
    0 讨论(0)
  • 2020-11-21 07:13

    It's rather easy to add some syntactical sugar that allows one to compose strings on the fly in a stream-like way

    #include <string>
    #include <sstream>
    
    struct strmake {
        std::stringstream s;
        template <typename T> strmake& operator << (const T& x) {
            s << x; return *this;
        }   
        operator std::string() {return s.str();}
    };
    

    Now you may append whatever you want (provided that an operator << (std::ostream& ..) is defined for it) to strmake() and use it in place of an std::string.

    Example:

    #include <iostream>
    
    int main() {
        std::string x =
          strmake() << "Current time is " << 5+5 << ":" << 5*5 << " GST";
        std::cout << x << std::endl;
    }
    
    0 讨论(0)
  • 2020-11-21 07:13

    Use:

    #define convertToString(x) #x
    
    int main()
    {
        convertToString(42); // Returns const char* equivalent of 42
    }
    
    0 讨论(0)
  • 2020-11-21 07:14

    sprintf() is pretty good for format conversion. You can then assign the resulting C string to the C++ string as you did in 1.

    0 讨论(0)
提交回复
热议问题