C++ template

template可以让编译器帮用户写代码,它将会被编译根据你具体的使用,it makes compiler write some code for you based some rules ,and sounds cool !当你使用模板编程时,编译器为此生成一个框架,当你传入某个具体的参数时,编译器将会根据你传入的参数在事先确定的框架里面写入代码。

为什么需要模板

当我们需要许多函数,其内部执行的操作是相似的,但是我们可能需要接受不同类型的参数,这样的函数就可以写模板

模板函数

只有当调用这个模板时,才会被写进代码(编译的时候写入源码),调用时会根据调用的具体的参数生成对应的代码,以下的函数可以接受任意标准类型的参数

template <typename T>
void print(T value){
    std::cout << value << std::endl;
}

我们就可以传入不同类型的参数调用这个函数,模板函数还接受模板参数,比如下面的int,也可不指定模板函数的模板参数,编译器进行自动推导

print<int>(10);
print("hello");

注意当我们没有调用这个函数时,这个函数并不会存在于源码中直到我们调用它,我们甚至可以将源码中变量value更改为valu,而编译并不会报错(在不调用该函数的前提下,在某些编译器下可能也会报错)

template <typename T>
void print(T value){
    std::cout << valu << std::endl;
}

当我们将模板函数传入某个具体的参数时,相当于增加了如下的代码,(编译器为我们写入的代码)

void print(int value){
    std::cout << value << std::endl;
}

当我们传入不同类型的参数时,编译器会为我们写入对应于不同类型的函数,即这个函数不同类型的多个版本

模板类

假如我们想在栈区声明一个数组,我们可以通过模板参数来指定数组的大小,因为这是在编译时被指定的

template<int N>
class array{
private:
	int arr[N];
public:
	int GetSize()const {return N};
}

当我们声明一个具体的对象并传入模板参数时

array<5> a;

我们就相当于定义了一个如下的类

class array{
private:
	int arr[5];
public:
	int GetSize()const {return 5};
}

此外,我们还可以声明多个模板参数,这样我们不仅可以指定数组的大小,我们还可以指定数组所存储元素的具体类型

template <typename T,int N>
class array{
private:
	T arr[5];
public:
	int GetSize()const {return 5};
}
  • 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、付费专栏及课程。

余额充值