C/C++_log2000_函数模板与类模板笔记2
来源:互联网 发布:java的形参和实参 编辑:程序博客网 时间:2024/04/28 23:47
函数模板与类模板的相关笔记 entry2
函数模板; 类模板;
类模板的一般定义形式为:
template <类型形参表> class 类名{ 类声明体;}建立类模板之后,可以用类型实参定义模板类,并创建实例。模板类是一个实实在在的类,对象是该模板类的势力。其格式如下:类名 <类型实参表> 对象;
下面看几个例子
例一 :定义数组类模板
#include <iostream>using namespace std;template <class T>class Array{private: T *arr;public: Array(int c) { arr = new T[c]; } void init(int i, T x) { arr[i] = x; } T& operator[](int i) { return arr[i]; }};void main(){ Array<int> array(5); cout << "input every element's value: " << endl; for (int i = 0; i < 5; i++){ cin >> array[i]; } cout << "every element's value in Array is: " << endl; for (i = 0; i < 5; i++){ cout << "No " << i << " : " << array[i] << endl; }}-------------------------------------运行结果:input every element's value:6 21 27 17 40every element's value in Array is:No 0 : 6No 1 : 21No 2 : 27No 3 : 17No 4 : 40
例二 类模板参数是类
类模板的参数不仅可以是标准数据类型,还可以是用户自定义类型,当然包括用户自己定义的类,如下所示:
#include <iostream>using namespace std;class A{private: int j;public: A(){} A(int x):j(x){} A(A* x) { j = x->j; } void operator!() { cout << "j = " << j << endl; }};template<typename T>class B{private: int i; T* x;public: B(int xa, T* p): i(xa){ x = new T(p); } void operator!() { cout << "i = " << i << endl; !*x; }};void main(){ A a(1); B<A> b(2, &a); !b;}----------------------------------运行结果:i = 2j = 1
例三
#include <iostream>using namespace std;template<class T>class Stack{public: Stack(int = 10); //default constructor(stack size 10) ~Stack(){ delete[]stackPtr; //destructor } bool push(const T&); //push an element onto the stack bool pop(T&); //pop an element off the stackprivate: int size; //# of elements in the stack int top; //location of the top element T* stackPtr; //pointer to the stack bool isEmpty() const{ return top == -1; } bool isFull() const{ return top == size-1; }};//Constructor with default size 10template<class T>Stack<T>::Stack(int S){ size = S > 0 ? S : 10; top = -1; //Stack is initially empty stackPtr = new T[size]; //allocate space for elements}//Push an element onto the stack//return true if successful false otherwisetemplate <class T>bool Stack<T>::push(const T& pushValue){ if (!isFull()){ stackPtr[++top] = pushValue; //place item in Stafck return true; //push successful } return false;}//Pop an element off the stacktemplate<class T>bool Stack<T>::pop(T& popValue){ if (!isEmpty()){ popValue = stackPtr[top--]; //remove item from Stack return true; //pop successful } return false;}void main(){ Stack<double> doubleStack(5); double d = 1.1; cout << "Pushing elements onto doubleStack\n"; while (doubleStack.push(d)){ //success true returned cout << d << ' '; d += 1.1; } cout<< "\nStack is full. cannot push " << d << "\n\nPoping elements from doubleStack\n"; while (doubleStack.pop(d)){ //success true returned cout << d << ' '; } cout << "\nStack is empty.Cannot pop\n"; Stack<int> intStack; int i = 1; cout << "\nPushing elements onto intStack\n"; while (intStack.push(i)){ //success true returned cout << i << ' '; ++i; } cout<< "\nStack is full.Cannot push " << i << "\n\nPopping elements from intStack\n"; while (intStack.pop(i)){ //success true returned cout << i << ' '; } cout << "\nStack is empty.Cannot pop\n";}--------------------------------------------------运行结果:Pushing elements onto doubleStack1.1 2.2 3.3 4.4 5.5Stack is full. cannot push 6.6Poping elements from doubleStack5.5 4.4 3.3 2.2 1.1Stack is empty.Cannot popPushing elements onto intStack1 2 3 4 5 6 7 8 9 10Stack is full.Cannot push 11Popping elements from intStack10 9 8 7 6 5 4 3 2 1Stack is empty.Cannot pop
visitor tracker
阅读全文
0 0
- C/C++_log2000_函数模板与类模板笔记2
- C/C++_log2000_函数模板与类模板笔记1
- C/C++:函数模板与类模板
- [C/C++]模板函数与模板类
- C++---函数模板与类模板
- C/C++_log2000_函数声明与指针定义等零散知识点随手笔记
- C++,template,类模板和函数模板
- C++:函数模板、类模板及其特化
- C/C++学习笔记(2)--函数模板template<typename T>,类模板详解(1)
- 类模板与模板类(C++)
- C/C++_log2000_空白窗口2
- 【c/c++】函数模板和模板函数
- c++:函数模板与函数包装器
- c++---函数和类模板
- C++primer 阅读笔记-模板与泛型编程(函数模板)
- C++Template学习笔记之函数模板
- [C++] C++小笔记之模板函数
- (C/C++学习笔记)函数模板加强
- 面向对象思想
- NEUOJ 73 Problem: Ready to declare
- hdu 1597
- 机器学习方法篇(1)------概览
- [YTU]_2803( 判断字符串是否为回文)
- C/C++_log2000_函数模板与类模板笔记2
- 前端开发-Web存储
- 1025. 反转链表 (25)
- Linux网络编程 基于tcp的多线程的服务器
- HDU
- Java中ArrayList内部实现
- jquery基础知识
- MongoDB 目录
- python基本语法:列表(列表和元组的区别)