【C++】60 数组类模板

Slide2.PNG

数值型模板参数的限制

-变量不能作为模板参数
-浮点数不能作为模板参数
-类对象不能作为模板参数
例:

#include <iostream>
#include <string>

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>::VALVE +N;
};
int main()
{  
 
    func<int,10>();
       return 0;
}

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

评论

暂无评论