【C++基礎】 類模板

類模板

模板是將類中某些類型變為泛型,從而定義一個模板。
如下:
在這里插入圖片描述

類模板的語法

直接進行對比:

泛型化之前
泛型化之后

類模板的實例化

注意:只要是對類模版進行實例化,編譯器就會生成一個類!!!
顯式實例化:

template class Stack<int>;  // 將類模板實例化為一個處理int類型的Stack類

隱式實例化:
生成一個處理char類型的對象:

Stack<char>  charStack; // 先實例化一個CharStack類(名字由編譯器按規則生成)// class CharStack { … char elements[100]; … };// 然后用 CharStack charStack; 創建一個對象

code附錄

泛型化之前的:

#pragma once//編寫StackOfIntegers類
class Stack {
private:int elements[100];int size{ 0 };
public:bool empty();int peek();int push(int value);int pop();int getSize();Stack();
};
Stack::Stack() {size = 0;for (int& i : elements) {i = 0;}
}bool Stack::empty() {return (size == 0 ? true : false);
}int Stack::getSize() {return size;
}int Stack::peek() {return elements[size - 1];
}int Stack::pop() {int temp = elements[size - 1];elements[size - 1] = 0;size--;return temp;
}int Stack::push(int value) {elements[size] = value;size++;return value;
}

泛型化之后的:

#pragma once//泛型化
template <typename T>
//編寫StackOfIntegers類
class Stack {
private:T elements[100];int size{ 0 };
public:bool empty();T peek();T push(T value);T pop();int getSize();Stack();
};
template <typename T>
Stack<T>::Stack() {size = 0;for (auto & i : elements) {i = 0;}
}
template <typename T>
bool Stack<T>::empty() {return (size == 0 ? true : false);
}
template <typename T>
int Stack<T>::getSize() {return size;
}
template <typename T>
T Stack<T>::peek() {return elements[size - 1];
}
template <typename T>
T Stack<T>::pop() {T temp = elements[size - 1];elements[size - 1] = 0;size--;return temp;
}
template <typename T>
T Stack<T>::push(T value) {elements[size] = value;size++;return value;
}

main測試代碼

#include "Stack.h"
#include <iostream>
#include <string>
int main()
{Stack<char> c;std::string s{ "Hello World!"};for (auto i : s) {c.push(i);}while (c.empty() != true) {std::cout << c.pop() << std::endl;}
}

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

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

相關文章

ruby 怎么拋異常_Ruby中的異常處理

ruby 怎么拋異常Ruby異常處理 (Ruby Exception Handling) Exceptions are abnormal conditions arising in the code segment at runtime in the form of objects. There are certain predefined Exception classes whereas we can create our exception known as Custom excep…

cocos2d-x游戲開發系列教程-中國象棋02-main函數和歡迎頁面

之前兩個博客講述了象棋的規格和工程文件之后&#xff0c;我們繼續深入的從代碼開始學習cocos2dx首先從程序入口main函數開始main函數int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow) {UNREFERENCED_PARAMETER(h…

[原創]Android中的android:layout_width和android:width的區別

在android系統中&#xff0c;我們可以通過在xml資源文件中定義布局&#xff0c;一般的寫法是&#xff1a; <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"ma…

【C++基礎】模板參數與模板繼承

模板參數 默認類型參數 函數參數可以設定一個默認值&#xff0c;我們現在可以對類模板的類型參數設定一個默認類型。 指定泛型Stack的默認類型參數為 int template<typename T int> class Stack{... };當我們這樣定義一個對象時&#xff1a; Stack<> stack;使…

UNIX標準化及實現之POSIX標準可選頭文件

POSIX標準定義的可選頭文件 頭文件說明<aio.h>異步I/O<mqueue.h>消息隊列<pthread.h>線程<sched.h>執行調度<semaphore.h>信號量<spawn.h>實時spawn接口<stropts.h>XSI STREAMS接口<trace.h>事件跟蹤轉載于:https://www.cnblo…

Julia中的denominator()函數

Julia| 分母()函數 (Julia | denominator() function) denominator() function is a library function in Julia programming language, it is used to get the denominator of the rational representation of the given value. denominator()函數是Julia編程語言中的庫函數&a…

【C++基礎】STL迭代器

已知&#xff1a; STL組成部分&#xff1a; 容器、迭代器、算法、函數對象、空間分配器 容器&#xff1a;用于保存一組數據&#xff0c;數據個體被稱為元素 迭代器&#xff1a;用于遍歷容器中的元素&#xff0c;容器都有自己專屬的迭代器&#xff0c;只有容器才知道如何遍歷自己…

用ie9瀏覽器若出現看視頻有聲音沒圖像的問題處理

當我們在用ie9瀏覽器上網想看視頻時&#xff0c;有時會遇到各種問題&#xff0c;尤其是有關聲音和圖像的。有時候有聲音沒圖像&#xff0c;但有時候有圖像卻沒聲音。各種問題。當遇到某些問題時&#xff0c;只要是關于網頁視頻的&#xff0c;一般都會選擇更新網頁視頻播放插件&…

java架構師之路:JAVA程序員必看的15本書的電子版下載地址

java架構師之路&#xff1a;JAVA程序員必看的15本書的電子版下載地址 作為Java程序員來說&#xff0c;最痛苦的事情莫過于可以選擇的范圍太廣&#xff0c;可以讀的書太多&#xff0c;往往容易無所適從。我想就我自己讀過的技術書籍中挑選出來一些&#xff0c;按照學習的先后順序…

office數據集dslr_DSLR的完整形式是什么?

office數據集dslrDSLR&#xff1a;數碼單鏡反光 (DSLR: Digital Single-Lens Reflex) DSLR is an abbreviation of digital single-lens reflex. It alludes to a digital camera which with the sensor of digital imaging merges optics and mechanism of single-lens reflex…

envs\TensorFlow2.0\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning 解決方案

import tensorflow后的完整報錯&#xff1a; D:\Anaconda3\envs\TensorFlow2.0\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will b…

轉義序列

轉義序列描述\n換行符\r回車\t水平制表符\\反斜杠\$美元符\"雙引號\[0-7]{1.3}八進制記法\x[0-9A-Fa-f]{1,2}十六進制記法轉載于:https://www.cnblogs.com/cindylu520/archive/2012/07/05/2577246.html

Java動態代理模擬spring的AOP

廣州瘋狂軟件學院擁有三大課程體系包括&#xff1a;java課程&#xff0c;android課程&#xff0c;ios課程&#xff0c;瘋狂軟件年終鉅惠&#xff0c;報名java就業班&#xff0c;免費贈送基礎班&#xff0c;名額有限&#xff0c;本月火熱報名中&#xff0c;歡迎有志之士電話或者…

xlrd.biffh.XLRDError: Excel xlsx file; not supported解決方法

將原本的xlrd卸載&#xff0c;安裝舊版本&#xff1a; pip uninstall xlrd pip install xlrd1.2.0轉自&#xff1a;https://www.cnblogs.com/xiaoqiangink/p/14144517.html

生產消費是什么設計模式_快速消費品的完整形式是什么?

生產消費是什么設計模式快消品&#xff1a;快速消費品 (FMCG: Fast-Moving Consumer Goods) FMCG is an abbreviation of Fast-Moving Consumer Goods which are also known as Consumer Packed Goods (CPG). These consumer packed goods allude to the products that are sol…

分類釋義概述

分類(classification) 是人工智能領域基本的研究領域之一&#xff0c;也是知識表示和獲取的主要途徑之一。一般認為&#xff0c;分類屬于科學發展的較初級階段&#xff0c;即形成理論之前的階段。 分類的釋義&#xff1a; 中文解釋&#xff1a;分類指的是將無規律的事物按照其性…

占位博客

占位博客 轉載于:https://www.cnblogs.com/CharmingDang/p/9663895.html

通過從全局和類內部重載operator new /delete來獲取內存管理權

目錄1、通過重載獲得內存管理權2、容器的內存管理3、重載new、array new、replacement new&#xff0c;接管內存控制權1、重載全局::operator new / ::operator delete以及array版本2、在類里面去重載1、通過重載獲得內存管理權 之前的幾章學習&#xff0c;是紅色的路線。此時…

sml完整形式_教資會的完整形式是什么?

sml完整形式教資會&#xff1a;大學教育資助委員會 (UGC: University Grants Commission) UGC is an abbreviation of the University Grants Commission. It is an organization established by the Indian Union government in agreement with the UGC Act 1956 under the Mi…

ASP.NET線程相關配置

1、ASP.NET 同一時刻只能發起的工作線程數量&#xff1a; (maxWorkerThreads * CPU邏輯數量&#xff09;-minFreeThreads 比如2個CPU默認配置maxWorkerThreads100&#xff0c;minFreeThreads176&#xff0c;則同時最大只能有24個工作線程。&#xff08;這里不管 <system.ne…