歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++拾遺--函數模板

C++拾遺--函數模板

日期:2017/3/1 9:32:56   编辑:Linux編程

前言

泛型的核心思想是數據與算法分離。函數模板是泛型編程的基礎。

函數模板

函數模板以 template<arg_list> 開頭,arg_list是泛型參數的列表。

1.模板的泛型參數個數確定

實例一

下面是一個加法函數模板,在實例化時,我們傳入普通的數據類型。

#include <iostream>
using namespace std;

template<typename T1, typename T2>
auto add(T1 t1, T2 t2)->decltype(t1 + t2)
{
return t1 + t2;
}
int main()
{
cout << add(12.3, 12) << endl;
cout << add(12, 12.3) << endl;
cin.get();
return 0;
}

運行

實例二

我們也可以傳入函數類型。

#include <iostream>
using namespace std;

template<typename T, typename F>
void exec(const T &t, F f)
{
f(t);
}
int main()
{
exec("calc", system);
cin.get();
return 0;
}

運行 system("calc"); 打開計算器

2.模板的泛型參數個數不確定

#include <iostream>
#include <cstdarg>
using namespace std;

//這個空參的函數用於遞歸終止
void show()
{

}
//參數個數可變,參數類型也多樣
template<typename T, typename...Args> //typename...Args是可變類型列表
void show(T t, Args...args)
{
cout << t << ends;
show(args...);
}
int main()
{
show(1, 2, 3, 4); cout << endl;
show('a', 'b', 'c', 'd');
cin.get();
return 0;
}

運行

下面利用函數模板來簡單的模仿下printf()函數

#include <iostream>
#include <cstdarg>
using namespace std;

void PRINTF(const char *format)
{
cout << format;
}
template<typename T, typename...Args>
void PRINTF(const char *format, T t, Args...args)
{
if (!format || *format == '\0')
return;
if (*format == '%') //處理格式提示符
{
format++;
char c = *format;
if (c == 'd' || c == 'f' || c == 'c' || c == 'g') //我們暫且只處理這幾種,其它的同理
{
cout << t;
format++;
PRINTF(format, args...);
}
else if (c == '%')
{
cout << '%';
format++;
PRINTF(format, t, args...);
}
else
{
cout << *format;
format++;
PRINTF(format, t, args...);
}
}
else
{
cout << *format;
PRINTF(++format, t, args...);
}
}
int main()
{
PRINTF("%asdljl%5234la;jdfl;\n");
PRINTF("%d alsd, %fasdf..%g..%c\n", 12, 3.4, 5.897, 'a');
cin.get();
return 0;
}

運行

利用函數模板簡易模擬printf()代碼下載

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2015年資料/2月/19日/C拾遺--函數模板/

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------分割線------------------------------

C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm

讀C++ Primer 之構造函數陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm

讀C++ Primer 之智能指針 http://www.linuxidc.com/Linux/2011-08/40177.htm

讀C++ Primer 之句柄類 http://www.linuxidc.com/Linux/2011-08/40175.htm

將C語言梳理一下,分布在以下10個章節中:

  1. Linux-C成長之路(一):Linux下C編程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成長之路(二):基本數據類型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成長之路(三):基本IO函數操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成長之路(四):運算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成長之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成長之路(六):函數要義 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成長之路(七):數組與指針 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成長之路(八):存儲類,動態內存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成長之路(九):復合數據類型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成長之路(十):其他高級議題

Copyright © Linux教程網 All Rights Reserved