c++面向對象高級編程 學習十一 類模板、函數模板、成員模板

namespace經驗談:
團隊中函數或類的名字可能會沖突,因此使用namespace進行區分。
在這里插入圖片描述
類模板:
template<typename T>
在這里插入圖片描述
函數模板:
template<class T>,此處class可改成typename
函數模板在使用的時候,不必指明類型,因為函數模板是去調用該函數,會在調用的時候傳入參數,編譯器會對函數模板進行參數推導,而類模板則需指明類型。
在這里插入圖片描述

成員模板:
在一個類中有一個成員,該成員是template,則稱該成員為成員模板。
在這里插入圖片描述
在這里插入圖片描述

template <class U1,class U2>
pair(const pair<U1,U2>&p):
first(p.first),second(p.second){}

該pair類允許任意的T1,T2,也允許任意的U1,U2,但是在編譯的過程中必須滿足first(p.first),second(p.second)的初始化賦值過程,在該示例中即,鯽魚,麻雀初始化賦值到first和second中沒有問題,而反之則編譯不能通過。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446600.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446600.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446600.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

操作系統面試 總結

以下文章來源于程序員cxuan &#xff0c;作者cxuan 原文鏈接什么是操作系統 操作系統是管理硬件和軟件的一種應用程序。操作系統是運行在計算機上最重要的一種軟件&#xff0c;它管理計算機的資源和進程以及所有的硬件和軟件。它為計算機硬件和軟件提供了一種中間層&#xff…

英語口語week 14 Thursday

英語文章 A couple decided to go out to celebrate their wedding anniversary, so they called a babysitter. When the babysitter arrived, the two children had already been asleep. The babysitter soon got bored and went to the kitchen where she blended some wh…

c++面向對象高級編程 學習十二 模板

模板特化&#xff1a; 模板是一種泛化的形式&#xff0c;特化是將參數類型進行指定&#xff0c;寫出特化的版本&#xff0c;當在調用下圖cout<<hash()(1000);的時候&#xff0c;由于特化中有struct hash{ }的版本&#xff0c;因此會直接調用特化部分。 模板偏特化&…

英語口語 week14 Friday

英語文章 Shopping is taking place every second. However, the prices of the same goods may differ from store to store. A name-brand dress may cost several hundred pounds at a boutique, but only half the price in a discount store or a big chain store. Moreo…

c++面向對象高級編程 學習十三 數量不定的模板參數,auto,for

文章目錄數量不定的模板參數autoranged-base for數量不定的模板參數 void print() {} //... 表示參數的數量不定 template<typename T, typename...Types> void print(const T&firstArg, const Types&...args) {cout << firstArg << endl;cout<&…

數據結構 樹

定義 樹是節點的優先集合度&#xff1a;孩子的數量&#xff0c;度為0 就是終端節點&#xff0c;不為零就是根節點有序樹&#xff1a;有順序&#xff0c;不可以替換無序樹&#xff1a;無順序&#xff0c;可以替換深度 和 樹的深度相反&#xff0c;第一層深度為1 樹的深度為 3 二…

英語口語 Week15 TuesDay

英語文章 One day, when Bella was doing sports in the school yard, the squirrel fled out of her sleeve. Threading its way through a considerable number of people, the squirrel disappeared in the distance After a sequence of movements, it hopped onto the ar…

c++面向對象高級編程 學習十四 引用

文章目錄referencereference的常見用途reference 變量有三種形式&#xff1a;值&#xff0c;指針&#xff0c;引用 int x0; //值 int* p&x;//指向整型的指針&#xff0c;地址&#xff0c;指針在之后的程序中可以指向其他變量 int& rx;//引用&#xff0c;此處表示 r代…

google瀏覽器 隱藏功能開啟

網址 chrome://flags/ 1&#xff0c;多線程下載 2&#xff0c;暗黑模式3&#xff0c;標簽縮略圖4&#xff0c;PWA 漸進式web應用 網頁即應用5&#xff0c;閱讀模式&#xff0c;排除廣告&#xff0c;點擊閱讀模式去除干擾chrome://net-internals6&#xff0c;解決有問題的代理IP…

英語口語Week 15 Wednesday

英語文章 Accomplishing the task assigned by the teacher; Julia rushed out. Squatting at the gate and playing with the squirrel, Bella waved at the sight of Julia and yelled out here" . Julia ran quickly towards them, pointed at the squirrel and asked…

c++面向對象高級編程 學習十五 組合繼承關系下的構造和析構

文章目錄繼承關系組合關系繼承和組合繼承關系 構造由內而外&#xff0c;析構由外而內&#xff0c;內即是父類 組合關系 A擁有B&#xff0c; 構造由內而外&#xff0c;析構由外而內&#xff0c;內即是B 繼承和組合 構造和析構順序如圖&#xff1a;

英語口語Week16 Wednesday

英語文章 Recently my friend received a gift from her boyfriend - a very expensive bracelet. But the substance of her response left us in astonishment - she didn’t attend to the exquisiteness(of the gift and wanted to return it to him In terms of salary, …

C++ 查漏補缺

特性關系 C語言面向過程C面向過程 面向對象(封裝 繼承 多態)C具備C語言的全部特性的基礎上&#xff0c;并且支持更多新的特性 內存泄露 申請內存&#xff0c;沒有釋放申請 malloc new釋放 free deleteProcessExplorer查看內存是否釋放 代碼移植 將生成的exe運行在別的平臺&…

c++面向對象高級編程 學習十六 vptr和vtbl

當一個類中有一個或多個虛函數時&#xff0c;內存中會多一個虛指針&#xff08;vptr&#xff0c;virtual pointer&#xff09;&#xff0c;指向一個虛表&#xff08;vtbl&#xff0c;virtual table&#xff09; 父類有虛函數&#xff0c;則子類一定有虛函數 在下圖示意圖中&a…

英語口語Week16 Thursday

英語文章 It is an impossibility that everything runs smoothly in everyday life. Where there is trouble, there could be anxiety.Anxiety is a common phenomenon; you are not the only one carrying it. But, it could be somewhat poisonous if you don’t let it o…

c++面向對象高級編程 學習十七 const, new, delete

文章目錄常量成員函數new和delete常量成員函數 常量成員函數是不改變成員數據。 當成員函數的const和non-const版本同時存在時&#xff0c;const object只能調用const版本&#xff0c;non-const object只能調用non-const版本。因此&#xff0c;可以看出&#xff0c;const是函…

codeforces 467A-C語言解題報告

題目網址 題目解析 1.輸入n個房間,再每一行輸入現有的p個人,和一共可以容納的q人數,如果q-p>2則計數1 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h>int main() {int n0,p0,q0;int count0,i;scanf("%d",&n);for(i0;i&…

使用引用的方式交換數據的數值

#include <iostream>void swap(int &a,int &b){a ^ b;b ^ a;a ^ b; } int main(){int num1 10;int num2 20;swap(num1,num2);std::cout << num1 << std::endl;std::cout << num2 << std::endl; }

C++STL與泛型編程 侯捷 (1)

泛型編程&#xff08;Generic Programming&#xff0c;GP&#xff09;&#xff0c;就是使用template&#xff08;模板&#xff09;為主要工具來編寫程序。 重要網頁&#xff1a; http://www.cplusplus.com/ https://en.cppreference.com/w/ http://gcc.gnu.org/

codeforces 136A-C語言解題報告

題目網址 題目解析 1.輸入每個人給第幾個人發禮物(1–n), 輸出每個人收到第幾個人發的禮物 2.因為1–n,所以for循環也使用1–n output[input[j]]j; 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h> int main() {int n0;scanf("%d&…