format函数
学习C++ STL,写一个format函数。
define _CRT_SECURE_NO_WARNINGS
include
include
include
include <stdarg.h>
using namespace std;
std::string format(char const* const format, ...);
std::string format(char const* const format, ...)
{
va_list args;
int len;
char* buffer;
std::string str;
// retrieve the variable arguments
va_start(args, format);
len = _vscprintf(format, args) // _vscprintf doesn't count
+ 1; // terminating '\0'
buffer = (char*)malloc(len * sizeof(char));
if (0 != buffer)
{
vsprintf(buffer, format, args); // C4996
// Note: vsprintf is deprecated; consider using vsprintf_s instead
str = buffer;
free(buffer);
}
va_end(args);
return str;
}
用于测试 ,生成格式化文本时,可以方便写作。
————————————————
版权声明:本文为CSDN博主「衢州小风风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/aasmfox/article/details/127466586