利用stringstream将int、double等转换为string

#include <sstream>

int main()
{
    std::stringstream stream;

    int a = 789;
    stream << a;
    printf("%s\n", stream.str().c_str());
    stream.str("");     // 清理流,不能用stream.clear();

    int b = -789;
    stream << b;
    printf("%s\n", stream.str().c_str());
    stream.str("");

    double c = 789.789;
    stream << c;
    printf("%s\n", stream.str().c_str());
    stream.str("");

    system("pause");

    return 0;
}

789
-789
789.789

你可能感兴趣的:(String)