日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ template笔记(1)模板函数

發(fā)布時(shí)間:2024/4/11 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ template笔记(1)模板函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.定義函數(shù)模板

template <typename T> inline T const& max (T const& a, T const& b) {// if a < b then use b else use areturn a < b ? b : a; }

2.使用模板函數(shù)

#include <iostream> #include <string> #include "max.hpp"int main() {int i = 42;std::cout << "max(7,i): " << ::max(7,i) << std::endl;double f1 = 3.4;double f2 = -6.7;std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;std::string s1 = "mathematics";std::string s2 = "math";std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl; }

輸出結(jié)果

3.確定返回的參數(shù)

::max<double>(1,2); //ok//::max(1,1.2);//wrong::max(static_cast<double>(4),4.1);//ok

若兩個(gè)參數(shù)不正確,或者不支持模板定義的特性,編譯時(shí)則會出錯

4.多個(gè)模板參數(shù)

template <typename T1,typename T2> inline T1 const& max (T1 const& a, T2 const& b) {// if a < b then use b else use areturn a < b ? b : a; }

示例

double i = 42.1; std::cout << "max(7,i): " << ::max(7,i) << std::endl;

返回值是T1,所以返回是int類型,結(jié)果是42,出錯了

定義3個(gè)參數(shù),第3個(gè)參數(shù)用于表示返回值類型

template <typename T1,typename T2,typename T3> inline T3 const& max (T1 const& a, T2 const& b) {// if a < b then use b else use areturn a < b ? b : a; }

測試

double i = 42.1; std::cout << "max(7,i): " << ::max<int,double,double>(7,i) << std::endl;

返回正確的42.1

5.模板函數(shù)重載

// maximum of two int values inline int const& max (int const& a, int const& b) {return a < b ? b : a; }// maximum of two values of any type template <typename T> inline T const& max (T const& a, T const& b) {return a < b ? b : a; }// maximum of three values of any type template <typename T> inline T const& max (T const& a, T const& b, T const& c) {return ::max (::max(a,b), c); }int main() {::max(7, 42, 68); // calls the template for three arguments::max(7.0, 42.0); // calls max<double> (by argument deduction)::max('a', 'b'); // calls max<char> (by argument deduction)::max(7, 42); // calls the nontemplate for two ints::max<>(7, 42); // calls max<int> (by argument deduction)::max<double>(7, 42); // calls max<double> (no argument deduction)::max('a', 42.7); // calls the nontemplate for two ints }

特別注意::max<>(7, 42);這句的寫法?
注意:自動類型轉(zhuǎn)換

::max('a', 42.7); 只適用于常規(guī)函數(shù),'a’會完成自動類型轉(zhuǎn)換

6.字符串重載的例子

#include <iostream> #include <cstring> #include <string>// maximum of two values of any type template <typename T> inline T const& max (T const& a, T const& b) {return a < b ? b : a; }// maximum of two pointers template <typename T> inline T* const& max (T* const& a, T* const& b) {return *a < *b ? b : a; }// maximum of two C-strings inline char const* const& max (char const* const& a,char const* const& b) { return std::strcmp(a,b) < 0 ? b : a; }int main () {int a=7;int b=42;::max(a,b); // max() for two values of type intstd::string s="hey";std::string t="you";::max(s,t); // max() for two values of type std::stringint* p1 = &b;int* p2 = &a;::max(p1,p2); // max() for two pointerschar const* s1 = "David";char const* s2 = "Nico";::max(s1,s2); // max() for two C-strings }

注意每個(gè)重載函數(shù)必須存在著一定的差異.

7.總是把重載函數(shù)定義在調(diào)用之前

// maximum of two values of any type template <typename T> inline T const& max (T const& a, T const& b) {return a < b ? b : a; }// maximum of three values of any type template <typename T> inline T const& max (T const& a, T const& b, T const& c) {return max (max(a,b), c); // uses the template version even for ints } // because the following declaration comes// too late: // maximum of two int values inline int const& max (int const& a, int const& b) {return a < b ? b : a; }

3參數(shù)的max調(diào)用的是2參數(shù)模板函數(shù),而非最后定義的max非模板函數(shù)

8.std:string和char

#include <string>// note: reference parameters template <typename T> inline T const& max (T const& a, T const& b) {return a < b ? b : a; }int main() {std::string s;::max("apple","peach"); // OK: same type::max("apple","tomato"); // ERROR: different types::max("apple",s); // ERROR: different types }

“apple”會被轉(zhuǎn)成 char[6]數(shù)組,所以比較要求數(shù)組維度長度相同,第二條”tomato”與”apple”長度不符合,即出錯.

s是std:: string類型,而非char數(shù)組,所以也出錯

9.以非引用參數(shù)方式傳遞

#include <string>// note: nonreference parameters template <typename T> inline T max (T a, T b) {return a < b ? b : a; }int main() {std::string s;::max("apple","peach"); // OK: same type::max("apple","tomato"); // OK: decays to same type::max("apple",s); // ERROR: different types }

只有最后類型不符合的編譯不通過

總結(jié)

以上是生活随笔為你收集整理的c++ template笔记(1)模板函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。