void WritrLogText(const char * _Format,...)
{
std::string tmp;
va_list marker = NULL;
va_start(marker, _Format);
size_t num_of_chars = _vscprintf(_Format, marker);
if (num_of_chars >= tmp.capacity())
{
tmp.resize(num_of_chars + 1);
}
vsprintf_s((char *)tmp.data(), tmp.capacity(), _Format, marker);
va_end(marker);
cout << tmp.c_str(); //格式化后的最终结果
}
string A = "123AAA";
int B = 999;
WritrLogText("<%s###%d>", A, B);
//输出结果:<123AAA###999>
C++ string文本格式化函数
于 2021-12-14 14:14:12 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)