预备知识

  • 模板参数可以是数值型参数(非类型参数)
template
< typename T, int N >
void func()
{
    T a[N];                  // 使用模板参数定义局部数组
}
func<double, 10>();
  • 数值型模板参数的限制

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

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

有趣的面试题:
用你觉得最高效的方法求 1 + 2 + 3 + ... + N 的值!

编程实验: 数值类模板参数

#include <iostream>

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 = Sum<N-1>::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

编程实验: 数组模板类

Array.h

#ifndef _ARRAY_H_
#define _ARRAY_H_

template
< typename T, int N >
class Array
{
private:
    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();                 // Array 类可能被继承,析构函数声明为虚函数
};

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

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

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

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

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

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

#endif

main.cpp

#include <iostream>
#include "Array.h"

using namespace std;

int main()
{
    Array<double, 5> ad;
    
    for(int i=0; i<ad.length(); i++)
    {
        ad[i] = i * i;
    }
    
    for(int i=0; i<ad.length(); i++)
    {
        cout << ad[i] << endl;
    }
    
    return 0;
}
输出:
0
1
4
9
16

编程实验: 堆数组模板类

HeapArray.h

#ifndef _HEAPRRAY_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& operator = (const HeapArray& obj);
    HeapArray& self();
    ~HeapArray();
};

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

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

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

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

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

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

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

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

template
< typename T >
HeapArray<T>& HeapArray<T>:: operator = (const HeapArray<T>& obj)
{
    if( this != &obj )
    {
        T* pointer = new T[obj.m_length];
        
        if( pointer )
        {
            for(int i=0; i<obj.m_length; i++)
            {
                pointer[i] = obj.m_pointer[i];
            }
            
            m_length = obj.m_length;
            delete m_pointer;
            m_pointer = pointer;
        }    
    }
    return *this;
}

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

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

#endif

main.cpp

#include <iostream>
#include "HeapArray.h"

using namespace std;

int main()
{
    HeapArray<char>* pai = HeapArray<char>::NewInstance(10);
    
    if( pai != NULL )
    {
        HeapArray<char>& ai = pai->self();
        
        for(int i=0; i<ai.length(); i++)
        {
            ai[i] = i + 'a';
        }
        
        for(int i=0; i<ai.length(); i++)
        {
            cout << ai[i] << endl;
        }
    }
    
    delete pai;
    
    return 0;
}
输出:
a
b
c
d
e
f
g
h
i
j

小结

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

以上内容参考狄泰软件学院系列课程,请大家保护原创!


TianSong
737 声望139 粉丝

阿里山神木的种子在3000年前已经埋下,今天不过是看到当年注定的结果,为了未来的自己,今天就埋下一颗好种子吧