1.初始化
double gpa[5]={3.1,3.5,3.8,2.9.3.3};
valarray<double> v1;//double数组,长度为0
valarray<int> v2(8);//int数组长度为8
valarray<int> v3(10,8);//int数组,每个元素值为10,8个元素
valarray<double> v4(gpa,4);//double 数组,用gpa的值初始化,4个单位长度
valarray<int> v5={20,32,17,9};//c++11
2.常用方法
[],size(),sum(),max(),min()
3.模板类的声明
template <typename Type>
需要在每个函数定义时附加
template<tyname Type>
bool Stack<Type>::push(const Type &item){}
例:
template<class Type>
Class Stack{
private:
enum{MAX=10};
Type items[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type & item);
bool pop(Type & item);
};
template <class Type>
Stack<Type>::Stack(){
top=0;
}
template<class Type>
bool Stack<Type>::isempty(){
return top==0;
}
template<class Type>
bool Stack<Type>::isFull(){
return top==MAX;
}
template<class Type>
bool Stack<Type>::push(const Type & item){
if(top<MAX){
items[top++]=item;
return true;
}
return false;
}
template<class Type>
bool Stack<Type>::pop(Type & item){
if(top>0)
{
item=items[--top];
return true;
}
return false;
}
4.实例化
Stack<int> kernels;
Stack<string> colonels;
5.模板类的递归
ArrayTP<Array TP<int,5>,10> twodee;
等价于int twodee[10][5];
注意两者行列定义位置相反
6.模板类的默认值设定
template<class T1,class T2=int> class Topo{}
Topo<double,double> m1;
Topo<double>m2;//double int
7.隐式实例化
ArrayTP<int,100>stuff;//隐式实例化
ArrayTP<double,30> *pt;//未实例化,指针。
pt=new ArrayTP<double,30>//需要对象,实例化。
8.显式实例化
template class ArrayTP<string,100>//产生一个ArrayTP的类,会生成类的定义,方法定义,不管你是否建立了该类的对象
9.显式特殊化
template<typename T>
class SortedArray{}
需要自己定义T::operator>()
举例:
template<> class SortedArray<const char *>
10.部分特殊化
template<class T1,class T2>class Pair{}//普通模板
template<class T1>class Pair<T1,int>{}//T2为int,部分特殊化
template<>class Pair<int,int>//显式特殊化
Pair<double,double> p1;//普通模板
Pair<double,int>p2;//部分特殊化
Pair<int,int>p3;//显式特殊化
template<class T>
class Feeb{};
template<class *T>
class Feed;
Feeb<char>fb1;//T为char,普通模板
Feed<char *>fb2;//T为char*,第二个特殊化模板
部分特殊化的应用
template<class T1,class T2,class T3>class Trio{};
template<class T1,class T2>class Trio<T1,T2,T2>{};
template<class T1>class Trio<T1,T1*,T1*>{};
对应于
Trio<int,short,char *>g1;
Trio<int,short> t2;
Trio<char,char *,char *> t3;
11.模板的嵌套使用
template <typename T>
class beta{
private:
template<typename T>
class hold{
private:
V val;
public:
hold<V v=0):val(v){}
void show() const{cout<<val<<endl;}
V value()const{return val;}
};
hold<T> q;
hold<int> n;
public:
beta(T t,int i):q(t),n(i){}
template<typename U>
U balb(U u,T t){return n.Value()+q.Value()*u/t;}
void Show()const{q.show();n.show();}
};
12.模板作为参数
template<template <typename T>class Thing>class Crab//声明
Crab<King> legs;//调用
template <typename T>//等价于
class King{};
举例:
template<template <typename T> class Thing>
class Crab{
private:
Thing<int> s1;
Thing<double> s2;
public:
Crab();
bool push(int a,double x){return s1.push(a)&&s2.push(x);}
bool pop(int &a,double &x){return s1.pop(a)&&s2.pop(x);}
};
//主函数调用该类的Push,Pop函数,实现双栈功能
Crab<Stack> nebula;
13.模板类与友元函数
1.非模板友元
2.相关绑定模板友元,友元与对象有关
3.非绑定友元,每个实现与对应的友元。
举例
template <class T>
class HasFriend{
public:
friend void counts();
}
1.通过全局对象调用counts()
2.使用全局指针访问非全局变量
3.生成一个对象调用,可访问静态变量独立于对象。
定义
friend void report(HasFriend &);//不可以
friend void report(HasFriend<T> &);//绑定模板,可以
不同的模板实现都会生成各自的静态变量
绑定模板友元函数到模板类
1.在类定义前声明模板友元函数
template <typename T> void counts();
template <typename T> void report(T &);
2.重新定义
template <typename TT>
class HasFriendT{
friend void count<TT>();
friend void report<>(HasFriend<TT> &);
};
举例
//模板原型
template <typename T> void counts();
template<typename T> void reprot(T &);
//template class
template <typename TT>
class HasFriendT{
private:
TT item;
static int ct;
public:
HasFriendT(const TT & i):item(i){ct++;}
~HasFriendT(){ct--;}
friend void counts<TT>();
friend void report<>(HasFriendT<TT> &):
};
template <typename T>
int HasFriend<T>::ct=0;
template <typename T>
void counts(){
cout<<"template size:"<<sizeof(HasFriend<T>)<<";";
cout<<"template counts():"<<HasFriend<T>::ct<<endl;
}
template <typename T>
void report(T &hf)
{
cout<<hf.item<<endl;
}
//未绑定的模板函数对应的模板类
template <typename T>
class ManyFriend{
private:
T item;
public:
ManyFriend(const T & i):item(i){}
template<typename C,typename D>void show2(C &,D &);
};
template<typename C,typename D>friend void show2(C & c,D & d){
cout<<c.item<<","<<d.item<<endl;
}
int main(){
ManyFriend<int> hfi1(10);
ManyFriend<int> hfi1(20);
ManyFriend<double> hfdb(10.5);
cout<<"hfi1,hfi2:";
show(hfi1,hfi2);
cout<<"hfdb,hfi2:";
show2(hfdb,hfi2);
return 0;
}
//模板别名
typedef std::array<double,12> arrd;
//另一种特性
template<typename T>
using arrtype=std::array<T,12>;
arrtype<double> gallons;
arrtype<int> days;
arrtype<string> months;