C++ Template

模板是实现代码重用的一种方法。化可以实现类型参数化,即把类型定义为参数,从而实现了真正的代码可重用性。对模板的使用可以分函数模板和类模板,关系如下:

模板--->实例化--->{模板函数,模板类}

模板类--->实例化--->模板类对象

 

对于模板函数,还是比较好理解的,其声明格式如下:

template <class type>或template <typename type>

返回类型 函数名(参数表)

{

   函数体

}

例:

template <calss T>

T max(T x,T y)

{

   return (x>y)?x:y

}

main()

{

  int i=10,j=20;

  char c='a',b='k';

  cout<<max(i,j)<<endl;

  cout<<max(c,b)<<endl;

  ...

}

 

注意:

template <class type>或template <typename type>

返回类型 函数名(参数表)

以上格式中template和函数模板定义语句之间不允许插入其它语句。

另在模板函数中允许有多个参数,如template max(class T1,class T2)

 

函数模板的类型参数只有在该函数真正被调用到时才决定其具体数据类型,编译器将按最先遇到的实参类型生成一个模板函数,并用它对所有模板进行一致性检查。如在上例中如果存在:

int i,char c;   max(i,c);则该调用会产生错误,因为编译器将先按变量i将T解释为int 类型,遇到模板实参c不能解释为int类型时则产生错误。

解决办法可以添加一个非模板函数的原型,则系统会首先调用该函数来进行替换。

完整的例子如下:

template <calss T>

T max(T x,T y)

{

   return (x>y)?x:y

}

int max(int ,int );

main()

{

  int i=10,j=20;

  char c='a',b='k';

  cout<<max(i,j)<<endl;

  cout<<max(c,b)<<endl;

  max(i,c);

  max(c,i);

  ...

}

 

在C++中,函数模板与同名的非模板函数重载时,调用顺序如下:

1、首先寻找一个参数完全匹配的函数并调用该函数

2、其次寻找一个函数模板,将其实例化产生一个匹配的模板函数并调用

3、通过类型转换看是否存在可以匹配的函数或函数模板,存在则调用它

 

 

 

 

 

模板类

格式如下: 

template <class Type>

class 类名{

   ......

};

其中:template是声明模板的关键字,表示声明一个模板。Type是模板参数

调用格式:

类名 <实际的数据类型> 对象名

如下例:

template <class T>

class example{

    public:

       example();

       T getexample();

       example create();

};

在类定义类外成员函数时,即如果成员函数中存在着模板参数的调用,则需要在函数体外进行模板声明template <class T>,并且在函数名前的类名后缀上<T>,如下例:

template <class T>      

example <T>:: example()    

{}   

template <calss T>

T example <T>::getexample()

{}

example <T> example<T>::create()

{}

 

另模板类可以有多个模板参数,如上例中可定义为

template <class T1,class T2>

class example{

......

};

 

另:在学习过程中,也在网上查阅了相关的资料,还有不少书籍描述一个模板方面的知识,并且有些深入让人一时难以理解,不得不再次感叹这些计算机知识的延申真的是无休止的啊,只能是简单了解一下,以待将来再仔细学习了^_^

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ templates are a powerful feature of the C++ programming language that allow generic programming. Templates enable the creation of functions and classes that can work with different data types without the need for separate implementations for each data type. Templates are defined using the keyword "template" followed by a list of template parameters enclosed in angle brackets "< >". The template parameters can be either type parameters or non-type parameters, depending on whether they represent a data type or a value. For example, a type parameter might be used to specify the data type of a container class, while a non-type parameter might be used to specify the size of an array. Here is an example of a simple function template that returns the maximum of two values: ```c++ template<typename T> T max(T a, T b) { return a > b ? a : b; } ``` In this example, the "typename" keyword is used to indicate that T is a type parameter. The function can be used with any data type for which the ">" operator is defined. Templates can also be used to define class templates, which are similar to regular classes but can work with different data types. Here is an example of a simple class template for a stack: ```c++ template<typename T> class Stack { public: void push(T value); T pop(); private: std::vector<T> data_; }; template<typename T> void Stack<T>::push(T value) { data_.push_back(value); } template<typename T> T Stack<T>::pop() { T value = data_.back(); data_.pop_back(); return value; } ``` In this example, the class template is defined with a single type parameter T. The member functions push and pop are defined outside the class definition using the scope resolution operator "::". Templates are a powerful tool that can greatly simplify code and make it more reusable. However, they can also be complex and difficult to debug. It is important to use templates judiciously and to thoroughly test them with a variety of data types.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值