string.format in c++

string.format in c++

C#里面的string.Format用着很爽,可是在C++里面拼字符串好像就不那么便捷了。
对一般内置类型来说,sprintf当能满足大部分需求。而对于自定义类型而言,可以利用stringstream来曲线救国:

#include  < sstream >
string  foo()
{
    stringstream ss;
    ss 
<< "x:" << _x << "\ty:" << _y;
    
return ss.str();
}

当然了,这里_x,_y的类型均需实现了
friend ostream&  operator << (ostream&  s, ObjClass& m)

----------------华丽的分割线--------------

Input/output string stream class

ios_base
ios
istream
iostream
stringstream
ostream

stringstream provides an interface to manipulate strings as if they were input/output streams.

ref of stringstream from cplusplus.com

你可能感兴趣的:(string.format in c++)