方法一、使用可变参数模板封装
template<class... T>
void Printf_ex(char const* const _Format, const T& ...arg)
{
printf(_Format, arg...);
}
方法二、使用宏
#define Printf_ex2(format, ...) printf(format, ##__VA_ARGS__)
测试:
int main()
{
int A = 111;
float B = 222.123;
double C = 151.1321;
string D = "www.www.www";
string FFF = "FFF.FFF.FFF";
Printf_ex("打印:\r\n \
int:%d\r\n \
string1:%s\r\n \
float:%f\r\n \
double:%f\r\n \
string2:%s\r\n",
A, FFF.c_str(), B, C, D.c_str());
int AAAA = 999;
float BBBB = 888.777;
double CCCC = 666.555;
string DDDD = "RRR.YYY.HHH";
string FFFFFF = "PPP.PPP.MMM";
Printf_ex2("打印:\r\n \
int:%d\r\n \
string1:%s\r\n \
float:%f\r\n \
double:%f\r\n \
string2:%s\r\n",
AAAA, FFFFFF.c_str(), BBBB, CCCC, DDDD.c_str());
return 0;
}
输出: