c++ 学习笔记--模板与标准模板库(一)
来源:互联网 发布:开源直播app源码 编辑:程序博客网 时间:2024/05/24 04:16
代码出处:http://www.weixueyuan.net/view/6398.html
有时候在设计程序的时候会遇到这样一种情况:需要设计的几个类,其功能都是一样的,仅仅只是需要操作的数据类型不同。遇到这样的情况,固然可以将所有的类都设计一遍,但是在C++中,我们有更好的方法:设计一个模板类。假设我们需要创建一个数组类,该数组可能是整型数组,也可能是double类型数组、string类型数组等,为此我们需要设计一个数组的模板类。
template< class T >class array{public: array( int ); T & operator[]( int ); const T & operator[] ( int )const; int getlen()const{ return length; } ~array();private: array(){}; int length; T * num;};template < class T >array< T >::array( int n ){ num = new T[n]; length = n;}template < class T >array< T >::~array(){ delete[] num;}template< class T >T & array< T > ::operator[] (int i){ if( i < 0 || i >= length) throw string("out of bounds"); return num[i];}template< class T >const T & array< T > ::operator[] (int i) const{ if( i < 0 || i >= length) throw string("out of bounds"); return num[i];}template< class T >ostream & operator<<(ostream & out, const array <T> & A){ for(int i=0; i < A.getlen(); i++) out<< A[i] << " "; return out;}
template< class T >
该语句称为模板头,可以单独放在一行,也可以与后面的class array同放在一行,这都没有关系。模板头中的“T”我们称之为类参数,类参数表示将会以确定的数据类型替代之,任何有效的C++标识符均可以作为类参数,当然通常我们还是会用“T”来做类参数。
类模板是对类的抽象 目的是为了实现代码重用 对那些设计代码的过程实现步骤基本一样 的相似的类 抽象出来 到用的时候再传具体的类
注意区别:
类外:
template < class T >T & array< T >::array( int n ){ num = new T[n]; length = n;}
类内:
array( int n ){ num = new T[n]; length = n;}
template< class T, class S, class R>class test{public: S fun( R r);private: T x;};
类模板的实例化
array < int >表明用int类型来代替模板类中的类参数“T”,编译器会将模板类array中所有的类参数T都用int来代替。例如类中的私有成员变量“T * num;”会被替换为“int * num;”。对类中的成员函数也会进行相同的替换,如“T & operator[]( int );”将会被替换为“int & operator[]( int );”。
#include <iostream>using namespace std;template< class T >class array{public: array( int ); T & operator[]( int ); const T & operator[] ( int )const; int getlen() const { return length; } ~array();private: array(){}; int length; T * num;};template < class T >array< T >::array( int n ){ num = new T[n]; length = n;}template < class T >array< T >::~array(){ delete[] num;}template< class T >T & array< T > ::operator[] ( int i ){ if( i < 0 || i >= length ) throw string( "out of bounds" ); return num[i];}template< class T >const T & array< T > ::operator[] (int i) const{ if( i < 0 || i >= length) throw string( "out of bounds" ); return num[i];}template< class T >ostream & operator<<( ostream & out, const array <T> & A ){ for(int i=0; i < A.getlen(); i++) out<< A[i] << " "; return out;}int main(){ array< int > A(10); for(int i = 0; i < 10; i++) { A[i] = 2*i; } cout<<A<<endl; return 0;}
“array< int > A(10);”实例化之后创建了一个对象A,该对象是一个包含10个元素的整型数组。之后用一个for循环给数组赋初值,由于我们重载了下标操作符,因此可以凭借下标直接访问相应的数组元素。之后直接输出A数组的所有元素,此时调用的是输出操作符重载函数。
另外模板类可以以参数的形式出现在函数的参数列表中,例如例1中的输出操作符重载函数“template< class T > ostream & operator<<(ostream & out, const array <T> & A)”,该函数的第二参数是array <T>的引用,如果参数列表中有模板类,则函数前面必须加上模板头,在例1中的模板头为“template< class T >”。
0 0
- c++ 学习笔记--模板与标准模板库(一)
- C++学习笔记(一)函数模板与类模板
- 模板与标准模板库
- 【C++】 C++标准模板库(一) Vector
- 《C++ primer plus》第十六章:string类和标准模板库(一) 学习笔记
- C/C++学习笔记(十)标准模板库STL和string
- C++学习笔记(五)标准模板库STL
- 【学习笔记】解析STL(标准模板库)
- 21天学通C++--阅读笔记6(模板、标准模板库)
- 模板学习笔记(一)
- php学习笔记(一)php模板与控制器
- C++:STL(标准模板库)
- 笔记:标准模板库(STL)
- C++模板与标准模板库(STL库)介绍
- C++STL模板笔记(一)
- C++:标准模板库(STL)
- C++:标准模板库vector
- C++:标准模板库map
- Servlet基础知识
- SpringMVC 使用总结
- Android控件SwipeRefreshLayout实现下拉刷新上拉加载
- C# 抽象类
- 安卓工具类之app获取版本渠道信息工具类
- c++ 学习笔记--模板与标准模板库(一)
- phantomjs 入门尝试
- 17-并发服务器设计(并发、同步、互斥的区别)
- POJ 3928
- 2017北交校赛总结
- 教你如何把man的内容保存下来,而且不会存在乱码
- 【C#基础知识点】——结构(if else-if、switch-case)
- 剑指Offer——(1)二维数组中的查找
- 赛码网-基本算法-3.纳德尔测试