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;}


模板类至少有一个类参数,但是可以有多个类参数,每一个类参数前都必须有关键字class或者类型名。


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
原创粉丝点击