激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - C/C++ - C++中模板(Template)詳解及其作用介紹

C++中模板(Template)詳解及其作用介紹

2021-12-30 15:21我是小白呀 C/C++

這篇文章主要介紹了C++中模板(Template)的詳解及其作用介紹,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

概述

模板可以幫助我們提高代碼的可用性, 可以幫助我們減少開(kāi)發(fā)的代碼量和工作量.

C++中模板(Template)詳解及其作用介紹

 

函數(shù)模板

函數(shù)模板 (Function Template) 是一個(gè)對(duì)函數(shù)功能框架的描述. 在具體執(zhí)行時(shí), 我們可以根據(jù)傳遞的實(shí)際參數(shù)決定其功能. 例如:

int max(int a, int b, int c){
  a = a > b ? a:b;
  a = a > c ? a:c;

  return a;
}

long max(long a, long b, long c){
  a = a > b ? a:b;
  a = a > c ? a:c;

  return a;
}

double max(double a, double b, double c){
  a = a > b ? a:b;
  a = a > c ? a:c;

  return a;
}

寫成函數(shù)模板的形式:

template<typename T>
T max(T a, T b, T c){
  a = a > b ? a:b;
  a = a > c ? a:c;

  return a;
}

 

類模板

類模板 (Class Template) 是創(chuàng)建泛型類或函數(shù)的藍(lán)圖或公式.

#ifndef PROJECT2_COMPARE_H
#define PROJECT2_COMPARE_H

template <class numtype>  // 虛擬類型名為numtype
class Compare {
private:
  numtype x, y;
public:
  Compare(numtype a, numtype b){x=a; y=b;}
  numtype max() {return (x>y)?x:y;};
  numtype min() {return (x < y)?x:y;};
};

mian:

int main() {

  Compare<int> compare1(3,7);
  cout << compare1.max() << ", " << compare1.min() << endl;

  Compare<double> compare2(2.88, 1.88);
  cout << compare2.max() << ", " << compare2.min() << endl;

  Compare<char> compare3('a', 'A');
  cout << compare3.max() << ", " << compare3.min() << endl;

  return 0;

}

輸出結(jié)果:

7, 3
2.88, 1.88
a, A

 

模板類外定義成員函數(shù)

如果我們需要在模板類外定義成員函數(shù), 我們需要在每個(gè)函數(shù)都使用類模板. 格式:

template<class 虛擬類型參數(shù)>
函數(shù)類型 類模板名<虛擬類型參數(shù)>::成員函數(shù)名(函數(shù)形參表列) {}

類模板:

#ifndef PROJECT2_COMPARE_H
#define PROJECT2_COMPARE_H

template <class numtype>  // 虛擬類型名為numtype
class Compare {
private:
  numtype x, y;
public:
  Compare(numtype a, numtype b);
  numtype max();
  numtype min();
};

template<class numtype>
Compare<numtype>::Compare(numtype a,numtype b) {
  x=a;
  y=b;
}

template<class numtype>
numtype Compare<numtype>::max( ) {
  return (x>y)?x:y;
}

template<class numtype>
numtype Compare<numtype>::min( ) {
  return (x>y)?x:y;
}

#endif //PROJECT2_COMPARE_H

 

類庫(kù)模板

類庫(kù)模板 (Standard Template Library). 例如:

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


int main() {
  int i = 0;
  vector<int> v;
  for (int i = 0; i < 10; ++i) {
      v.push_back(i);  // 把元素一個(gè)一個(gè)存入到vector中
  }

  for (int j = 0; j < v.size(); ++j) {
      cout << v[j] << " ";  // 把每個(gè)元素顯示出來(lái)
  }

  return 0;
}

輸出結(jié)果:

0 1 2 3 4 5 6 7 8 9

 

抽象和實(shí)例

C++中模板(Template)詳解及其作用介紹

到此這篇關(guān)于C++中模板(Template)詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++模板內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/weixin_46274168/article/details/116504709?spm=1001.2014.3001.5501

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精久久 | 亚洲影视中文字幕 | 夜夜夜操操操 | 国产羞羞网站 | 免费黄色大片网站 | 91精品国产91久久久久久蜜臀 | 成人片免费视频 | 免费黄色小视频网站 | 91色成人| 91精品国产综合久久婷婷香蕉 | 国产成人精品一区二区视频免费 | 国产免费传媒av片在线 | 免费啪视频在线观看 | 中国大陆一级毛片 | 91免费播放| 亚洲一区二区三区四区精品 | 一级大片一级一大片 | 成年人视频免费 | 亚洲人成网站免费播放 | 日韩精品一区二区三区中文 | 亚洲成人在线视频网 | 黄色免费av| 色啪综合| 午夜影院a | 欧美成a人片在线观看久 | 久草最新网址 | 久久av免费 | 久久精品一区二区三区不卡牛牛 | 国产精品一区在线免费观看 | 午夜视频在线观看91 | 我爱我色成人网 | 免费在线国产 | 国产精品高潮99久久久久久久 | 国产在线久 | 国产99视频精品免视看9 | 在线免费观看精品 | 深夜免费视频 | 福利在线免费 | 欧美日韩高清不卡 | 免费午夜网站 | jizzjizzjizz少妇|