将Int、Double转化成std::string
CString中可以使用Format
#include "stdafx.h"
#include
#include
#include
template
std::string ConvertToString(T);
int main() {
std::string s;
// Convert int to std::string
int i = 123;
s = ConvertToString(i);
std::cout << s << std::endl;
// Convert double to std::string
double d = 123.123;
s = ConvertToString(d);
std::cout << s << std::endl;
getchar();
return 0;
}
template
std::string ConvertToString(T value) {
std::stringstream ss;
ss << value;
return ss.str();
}
=================================================================================
加MSDN上的例子
#include
void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );
printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
Output:
String: computer
Character: l
Integer: 35
Real: 1.732053
character count = 71
阅读(7032) | 评论(2) | 转发(0) |