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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

《C++ primer 》 猎豹网校 函数模板 2018/10/9

發布時間:2024/8/1 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《C++ primer 》 猎豹网校 函数模板 2018/10/9 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 函數模板 -----實例化-----函數
  • 模板參數

? ? ? ? ? ? ? ? template<typename T>

? ? ? ? ? ? ? ? ? template<class T>

  • 四個例子
// ConsoleApplication3.cpp: 定義控制臺應用程序的入口點。 // #include "stdafx.h" #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std;//int compare(const double &v1, const double &v2) //{ // if (v1 < v2) return -1; // if (v1 > v2) return 1; // return 0; //} // //int compare(const string &v1, const string &v2) //{ // if (v1 < v2) return -1; // if (v1 > v2) return 1; // return 0; //}template <typename T> int compare(const T &v1, const T &v2) {if (v1 < v2) return -1;if (v1 > v2) return 1;return 0; };template <class T> T absVal(T val) {return val > 0 ? val : -val; }template <typename T1,typename T2> T1& print(T1 & s, T2 val) {s << val;return s; }template<class T> const T& bigger(const T& v1, const T& v2) {return v1 > v2 ? v1 : v2; }int main() {double d1 = 1.23;double d2 = 9.56;cout << compare(d1, d2) << endl;string s1("hi");string s2("world");cout << compare(s1, s2) << endl;double dval = -0.88;float fval = -12.33;cout << absVal(dval) << endl;cout << absVal(fval) << endl;string orister = "this is a test";string desstr;ostringstream oss(desstr); //輸出字符串流ofstream outFile("result.dat"); //輸出文件流print(cout, -3) << endl;print(cout, dval) << endl;print(cout, fval) << endl;print(cout, orister) << endl;print(outFile, -3) << endl;print(outFile, dval) << endl;print(outFile, fval) << endl;print(outFile, fval) << endl;print(outFile, orister) << endl;outFile.close();print(oss, -3) << endl;print(oss, dval) << endl;print(oss, fval) << endl;print(oss, orister) << endl;double a = 1.23;double b = 5.39;cout << bigger(a, b) << endl;system("pause");return 0; }

?

總結

以上是生活随笔為你收集整理的《C++ primer 》 猎豹网校 函数模板 2018/10/9的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。