//簡單實現C語言中printf函數
#include<iostream>
#include<typeinfo>
#include<string.h>
using namespace std;
void print(const char* str)//處理只有字符串的時候
{
cout << str << endl;
}
template <class T, class... Args>
void print(const char* str, T t, Args... args)
{
if((*str) == '\0' || str == NULL)//退出遞歸條件
{
return;
}
if(*str == '%')
{
switch(*(++str))//這裏只實現幾個代表一下
{
case 'd':
if(strcmp(typeid(t).name(), "i"))
{
cout << "參數類型不匹配!" << endl;
return;
};
break;
case 'c':
if(strcmp(typeid(t).name(), "c"))
{
cout << "參數類型不匹配!" << endl;
return;
};
break;
default:;break;
}
cout << t;
print(++str, args...);
}
else if(*str == ' ')
{
cout << ' ';
print(++str, t, args...);
}
else
{
cout << *str;
print(++str, t, args...);
}
}
int main()
{
return 0;
}
C++實現C語言printf函數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
C++智能指針模板類複習
//C++智能指針模板類複習 #include<iostream> #include<memory> using namespace std; //
linukey
2020-06-23 06:00:02
模板元編程加速遞歸
//模板元編程加速遞歸 //缺點:模板元在編譯時處理,無法調試,並且會加大代碼體積 #include<iostream> using namespa
linukey
2020-06-23 06:00:02
C++類轉換構造函數和轉換函數複習
//C++類轉換構造函數和轉換函數複習 #include<iostream> #include<string> using namespace st
linukey
2020-06-23 06:00:02
C++ lambda表達式複習
linukey
2020-02-23 19:47:32
C++中類const、static、static const成員變量
linukey
2020-02-23 19:47:32
C++ 在堆上開闢與釋放二維、三維指針
linukey
2020-02-23 19:47:32
利用包裝器以及lambda表達式實現二叉查找樹
linukey
2020-02-23 19:47:32
extern
yuwei19840916
2020-02-23 15:07:22
linux下查看so文件的函數列表
赵小刚
2020-02-22 22:52:11