C++ 自制string的format函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w442863748/article/details/52954037
#include <stdlib.h>
#include <iostream>
#include <list>
#include <conio.h>
#include <time.h>
#include <algorithm>
#include <windows.h>
#include <sstream>
//头文件引用较多, 有一些与本程序无关

static inline std::string format(const char *fmt,...)
{
#define FORMAT_MSG_BUFFER_SIZE (204800)
    char szBuffer[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 };
    va_list args;
    va_start(args, fmt);
    vsnprintf(szBuffer, FORMAT_MSG_BUFFER_SIZE, fmt, args);
    va_end(args);
    std::string strRet  = szBuffer;
    return strRet;
}

using namespace std;

int main(int argc, char* argv[])
{
	ostringstream oss;
	int i = 0;
	char tempstr[8];
	i = 20;
	oss << format("%04d", i);//输出结果为0020

	cout << oss.str() << endl;

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/w442863748/article/details/52954037