C++模版使用
2007-09-05 21:56
253 查看
一.基本
以下的模版类型关键字class在新C++标准中建议使用typename代替.
1.1通用函数
template <class Ttype> re-type func-name(parameter list)
{
//body of funtion
}
例如:
template <class X> void swap(X &a,X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
#include <iostream>
using namespace std;
int main()
{
int i=10, j=20;
swap(i, j);
cout<<"swapped i, j:"<<i<<j<<'\n';
}
1.2通用类
template <class Ttype> class class-name
{
//body of class
}
例如:
#include <iostream>
using namespace std;
const int SIZE = 10;
template <class StackType> class stack
{
StackType stack[SIZE];
int tos; //栈顶索引
public:
stack() { tos = 0;}
void push(StackType ob)
{
if(tos==SIZE)
{
cout<<"stack is full.\n";
return;
}
stack[tos]=ob;
tos++;
}
StackType pop()
{
if(tos==0)
{
cout<<"stack is empty.\n");
return 0;
}
tos--;
return stack[tos];
}
}
int main()
{
stack<char> s1;
s1.push('a');
s1.push('b');
while(s1.tos)
{
cout<<"pop s1:"<<s1.pop()<<'\n';
}
}
本质上,我认为模版实现了通用逻辑的定义,使算法独立于数据类型(由于实际程序一定要先给出数据类型,所以编译程序需要先解析模版,类似于宏命令).如上面的栈类,实际可能有整型栈或浮点型栈等.如果不使用模版,我们需要针对每个类型编一个栈类,其实操作又是完全一样.顺便说一下:STL就是一批高手精心设计的模版.
二.提高
2.1多类型模版
模版类型可以有多个,如:
template <typename OrgType, typename DesType> DesType covent(OrgType org)
{
return (DesType)org;
}
2.2重载
同类等类似,模版也可以重载.如:定义同名的函数重载函数模版或定义另一个同名但类型定义数不同的模版.
template<> void swap<int>(int &a, int &b)
{
int temp;
temp = a;
a =b ;
b = temp;
}
2.3默认类型
template <class X=int> class myclass
{
//body of class
}
2.4使用无类型参数
template <class Stype, int size> class stack
{
Stype stackBuffer[size];
... ...
}
调用时:
stack<int , 20> intStack;
2.5typenaem和export关键字
typename 通知编译器模版声明中使用的名字是类型名,而不是对象名,前面的模版类型关键字class可以用它代替.
export用在template声明之前,使其它文件只通过指定模版声明而不是全部复制来使用其它文件中定义的模版.
以下的模版类型关键字class在新C++标准中建议使用typename代替.
1.1通用函数
template <class Ttype> re-type func-name(parameter list)
{
//body of funtion
}
例如:
template <class X> void swap(X &a,X &b)
{
X temp;
temp = a;
a = b;
b = temp;
}
#include <iostream>
using namespace std;
int main()
{
int i=10, j=20;
swap(i, j);
cout<<"swapped i, j:"<<i<<j<<'\n';
}
1.2通用类
template <class Ttype> class class-name
{
//body of class
}
例如:
#include <iostream>
using namespace std;
const int SIZE = 10;
template <class StackType> class stack
{
StackType stack[SIZE];
int tos; //栈顶索引
public:
stack() { tos = 0;}
void push(StackType ob)
{
if(tos==SIZE)
{
cout<<"stack is full.\n";
return;
}
stack[tos]=ob;
tos++;
}
StackType pop()
{
if(tos==0)
{
cout<<"stack is empty.\n");
return 0;
}
tos--;
return stack[tos];
}
}
int main()
{
stack<char> s1;
s1.push('a');
s1.push('b');
while(s1.tos)
{
cout<<"pop s1:"<<s1.pop()<<'\n';
}
}
本质上,我认为模版实现了通用逻辑的定义,使算法独立于数据类型(由于实际程序一定要先给出数据类型,所以编译程序需要先解析模版,类似于宏命令).如上面的栈类,实际可能有整型栈或浮点型栈等.如果不使用模版,我们需要针对每个类型编一个栈类,其实操作又是完全一样.顺便说一下:STL就是一批高手精心设计的模版.
二.提高
2.1多类型模版
模版类型可以有多个,如:
template <typename OrgType, typename DesType> DesType covent(OrgType org)
{
return (DesType)org;
}
2.2重载
同类等类似,模版也可以重载.如:定义同名的函数重载函数模版或定义另一个同名但类型定义数不同的模版.
template<> void swap<int>(int &a, int &b)
{
int temp;
temp = a;
a =b ;
b = temp;
}
2.3默认类型
template <class X=int> class myclass
{
//body of class
}
2.4使用无类型参数
template <class Stype, int size> class stack
{
Stype stackBuffer[size];
... ...
}
调用时:
stack<int , 20> intStack;
2.5typenaem和export关键字
typename 通知编译器模版声明中使用的名字是类型名,而不是对象名,前面的模版类型关键字class可以用它代替.
export用在template声明之前,使其它文件只通过指定模版声明而不是全部复制来使用其它文件中定义的模版.
相关文章推荐
- C++ 在模版类里面使用STL 报错?
- C++之模版类的使用
- c++模版的一些使用方法(一)
- C++模版使用用例
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式
- C++模版使用技巧--Int2Type
- C/C++的预编译宏做成代码模版使用
- C++使用模版技术将任意类型的数据转为某个类型的数据
- C++模版使用
- C++使用模版实现按成员变量或者成员函数排序
- C++中template(模版)的使用
- c++模版的一些使用方法(三)
- c++模版的一些使用方法(四)
- C++中模板类使用友元模板函数 和友员非模版函数!使用不当出现编译错误:无法解析的外部符号…
- 【C++】模版的概念、使用方法和深入了解
- 浅谈C++中模版的使用
- C++模版的使用
- C++模版使用技巧--Int2Type
- c++ 模版的使用方法
- 使用模版元编程实现C和C++中复杂类型声明的语义化