#include <iostream>
#include <cstdio>
//字符串格式化
template <typename... Args>
std::string StrFormat(const char* format, Args... args)
{
int length = std::snprintf(nullptr, 0, format, args...);
if (length <= 0)
{
return "";
}
char* buf = new char[length + 1];
std::snprintf(buf, length + 1, format, args...);
std::string str(buf);
delete[] buf;
return std::move(str);
}
int main()
{
std::string t = StrFormat("this is a %s and %d", "hello", 1);
std::cout << t << std::endl;
}
c++ 标准库实现字符串格式化
最新推荐文章于 2024-04-20 22:03:52 发布