std::strstream和std::stringstream

开始

#include //std::stringstream(推荐使用)
#include //std::strstream(已废弃)
#include 

template
std::string ToString(Type_ num)
{
    std::ostringstream ss;
    ss << std::fixed;
    ss.precision(9);
    ss << num;
    return ss.str();
}

int main()
{
    double d = pow(10, 16) * 8;
    std::string xxxxx = ToString(d);

    const char* pStr = "test [std::strstream] and [std::stringstream],";
    int k = strlen(pStr);
    if (true)
    {
        std::strstream _strstream;
        std::string str;
        _strstream << pStr; //如果没有 std::ends, 会因为没有结尾符而在结尾出现乱码
        str = _strstream.str(); //返回值 char*
        std::cout << "1: " << str << std::endl;
    }
    if(true)
    {
        std::strstream _strstream;
        std::string str;
        _strstream << pStr << std::ends;
        str = _strstream.str(); //返回值 char*
        std::cout << "2: " << str << std::endl;
    }
    if(true)
    {
        std::stringstream _stringstream;
        std::string str;
        _stringstream << pStr;
        str = _stringstream.str(); //返回值 std::string
        std::cout << "3: " << str << std::endl;
    }
    return 0;
}

完。

你可能感兴趣的:(c/c++)