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 ) );
#include <sstream>
#define SSTR( x ) static_cast< std::ostringstream & >( \( std::ostringstream() << std::dec << x ) ).str()
用法尽可能简单:
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 ) );
#include <sstream>
int x = 42; // The integerstring str; // The stringostringstream temp; // 'temp' as in temporarytemp << x;str = temp.str(); // str is 'temp' as string