头文件:
#include <cstddef>
#include <iostream>
using namespace std;
template<class T,size_t N=10>
class Array{
T data[N];
size_t count;
public:
Array(){count = 0;}
void push_back(const T&t){
if(count<N )
data[count++]= t;
}
void pop_back(){
if(count>0)
--count;
}
T* begin(){return data;}
T* end(){return data+count;}
};
template<class T,template<class,size_t =10> class Seq>
class Container{
Seq<T> seq;//这里的Seq代表实例化的类 Array<float> arr;就是这样
public:
void append(const T& t){seq.push_back(t);}
T* begin(){return seq.begin();}
T* end(){ return seq.end();}
};
cpp文件:
#include "Container.h"
int main()
{
Container<int,Array> container;//用Array去实例化Seq,Seq如同形参
container.append(1);
container.append(2);
container.append(4);
int *p=container.begin();
while(p!=container.end())
cout<<*p++<<endl;
}