第60课-数组类模板

一、预备知识

模板参数可以是数值类参数(非类型参数)

template <typename T,int N>
void func(){
  T a[N];       //使用模板参数定义局部数组
}
···
func<double,10>();

数值型模板参数的限制

  • 变量不能作为模板参数
  • 浮点数不能作为模板参数
  • 类对象不能作为模板参数

本质:模板参数是在编译阶段被处理的单元,因此,在编译阶段必须准确无误地唯一确定

编程实验:有趣的面试题

#include
using namespace std;

template < typename T,int N>
void func(){
    T a[N] = {0};
    for(int i = 0;i < N;++i){
        a[i] = i;
    }
    for(int i = 0;i < N;++i){
        cout << a[i] << endl;
    }
}

template <int N>
class Sum{
    public:
        static const int Value = Sum1>::Value + N;
};

template <>
class Sum <1>{
    public:
        static const int Value = 1;
};

int main(){
    cout << "1 + 2 + 3 + ···+ 10 = " << Sum<10>::Value << endl;
    cout << "1 + 2 + 3 +  ···+ 100 = " << Sum<100>::Value << endl; 
    return 0;
} 

打印结果:

1 + 2 + 3 + ···+ 10 = 55
1 + 2 + 3 +  ···+ 100 = 5050

总结:之所以认为其高效,是因为数值型模板参数实在编译阶段被处理的单元,也就是说Sum<10>::Value在编译时就已经得出来了,程序实际运行时只是读取“常量”罢了。

二、数组模板类

#ifndef _ARRAY_H_
#define _ARRAY_H_

template <typename T,int N>
class Array{
    T m_array[N];
    public:
        int length();
        bool set(int index,T value);
        bool get(int index,T& value);
        T& operator[](int index);
        T operator[] (int index) const;
        virtual ~Array();
};

template <typename T,int N>
int Array::length(){
    return N;
}

template <typename T,int N>
bool Array::set(int index,T value){
    bool ret = (0 <= index) && (index < N);
    if( ret ){
        m_array[index] = value;
    }
    return ret;
}

template <typename T,int N>
bool Array::get(int index,T& value){
    bool ret = (0 <= index) && (index < N);
    if( ret){
        value = m_array[index];
    }
    return ret;
}

template <typename T,int N>
T Array::operator[] (int index) const{
    return m_array[index];
}

template <typename T,int N>
Array::~Array(){
}

#endif

三、堆数组模板类

#ifndef _HEAPARRAY_H_
#define _HEAPARRAY_H_

template <typename T>
class HeapArray{
    private:
        int m_length;
        T* m_pointer;

        HeapArray(int len);
        HeapArray(const HeapArray& obj);
        bool construct();
    public:
        static HeapArray* NewInstance(int length);
        int length();
        bool get(int index,T& value);
        bool set(int index,T value);
        T& operator [] (int index);
        T operator [] (int index) const;
        HeapArray& self();
        ~HeapArray(); 
};

template <typename T>
HeapArray::HeapArray(int len)
{
    m_length = len;
}

template <typename T>
bool HeapArray::construct(){
    m_pointer = new T[m_length];
    return m_pointer != NULL;
}

template <typename T>
HeapArray* HeapArray::NewInstance(int length){
    HeapArray* ret = new HeapArray(length);
    if(!(ret && ret->construct())){
        delete ret;
        ret = 0;
    }
    return ret;
}

template <typename T>
int HeapArray::length(){
    return m_length;
}

template <typename T>
bool HeapArray::get(int index,T& value){
    bool ret = (0 <= index) && (index < length());
    if( ret ){
        value = m_pointer[index];
    }
    return ret;
}

template <typename T>
bool HeapArray::set(int index,T value){
    bool ret = (0 <= index) && (index < length());
    if( ret ){
        m_pointer[index] = value;
    }
    return ret;
}

template <typename T>
T& HeapArray::operator [] (int index){
    return m_pointer[index];
}

template <typename T>
T HeapArray::operator [] (int index) const{
    return m_pointer[index];
}

template <typename T>
HeapArray& HeapArray::self(){
    return *this;
}

template <typename T>
HeapArray& HeapArray::self(){
    return *this;
}

template <typename T>
HeapArray::~HeapArray(){
    delete[] m_pointer;
}

#endif

主函数:main.cpp

#include 
#include 
#include "Array.h"
#include "HeapArray.h"

using namespace std;

int main()
{
    Array<double, 5> ad;

    for(int i=0; ifor(int i=0; icout << ad[i] << endl;
    }

    cout << endl;

    HeapArray<char>* pai = HeapArray<char>::NewInstance(10);

    if( pai != NULL )
    {
        HeapArray<char>& ai = pai->self();

        for(int i=0; i'a';
        }

        for(int i=0; icout << ai[i] << endl;
        }
    }

    delete pai;

    return 0;
}

四、总结

  • 模板参数可以是数值型参数
  • 数值型模板参数必须在编译期间唯一确定
  • 数组类模板参数是基于数值型模板参数实现的
  • 数组类模板是简易的线性表数据结构

你可能感兴趣的:(编程语言,--,C/C++,C++学习)