参数不一致实现printf 1 #include <iostream> 2 #include <cstdarg> 3 using namespace std; 4 5 //递归终止 6 void show(const char *str) 7 { 8 } 9 10 template<typename T,typename...Args> 11 void show(const char *str, T t, Args...args)//args如果没结束就当做T t,Args...args,如果结束则不是参数 12 { 13 while (str && *str) 14 { 15 if (*str == '%' && *(str + 1) != '%') 16 { 17 str++;//指针向后移动 18 cout << t;//打印 19 show(++str, args...); 20 return; 21 } 22 else if(*str == '%' && *(str + 1) == '%') 23 { 24 cout << *str++; 25 str++; 26 } 27 else 28 { 29 cout << *str++;//跳过一个字符 30 } 31 } 32 } 33 34 35 36 void main() 37 { 38 show("
