template 模板类入门

  • List item

最简单实例

template<typename N>
N addition(N a,N b) {
	return a + b;
}
int main() {
	cout << addition(2.3, 3.3);
	return 0;
}
//免去了写多个重载函数

模板的含义和作用

模板仅仅是声明了一段代码的描写叙述。不是一个能够直接运行代码,仅仅有依据实际情况用实參的数据类型取代类型參数标识符之后,才能运行。

模板本身就只是一个产生类和函数的模子。在C++中,模板就是一个代码的代码生产工具,在最终的代码中,根本就没有模板本身存在,只有模板具现出来的具体类和具体函数的代码存在。

疑惑1 class和typename区别?

答:class用于定义类,在模板引入c++后,最初定义模板的方法为:template,这里class关键字表明T是一个类型。后来为了避免class在这两个地方的使用可能给人带来混淆,所以引入了typename这个关键字。它的作用同class一样表明后面的符号为一个类型。这样在定义模板的时候就能够使用以下的方式了: template.在模板定义语法中关键字class与typename的作用全然一样。

疑惑2 类模板和模板类的区别?

模板类是类模板实例化后的一个产物,说个具体点的例子吧,我们把类模板比作是一个做饼干的模子,而模板类就是用这个模子做出来的饼干,至于这个饼干是什么味道的就要看你自己在实例化时用的是什么材料了,你可以做巧克力饼干,也可以做牛奶饼干,这些饼干出了材料不一样外,其它的东西都是一样的了。

疑惑3 函数模板和模板函数的区别?

同 类模板 和 模板类 的关系相同,函数模板实例化后得到的是模板函数
在运用的时候,可以显式(explicitly)生产模板函数,fun< int >、fun< double >、fun<Shape*>…
也可以在使用的过程中由编译器进行模板参数推导,帮你隐式(implicitly)生成。

疑惑4 模板函数的参数

(1)模板参数:位于函数模板名称的前面,在一对尖括号内部进行声明:

template <typename T>   // T是模板参数
template <typename T, int N> //T,N是模板参数
!!!!
所以模板参数可以是各种东西
实际上就是在下面的代码中挖了待填空格

(2)调用参数:位于函数模板名称之后,在一对圆括号内部进行声明:

max(T const& a, T const& b); 

每种参数个数都没有限制,对于模板参数,比如:

template <typename T1, typename T2>
max<int, double>(4, 4.3); 

疑惑5 什么是缺省参数?

所谓缺省参数,顾名思义,就是在声明函数的某个参数的时候为之指定一个默认值,在调用该函数的时候如果采用该默认值,你就无须指定该参数。缺省参数使用主要规则:调用时你只能从最后一个参数开始进行省略

注意:< typename T >也可以运用缺省参数,例如< typename T = int >,进行实例化的时候,如果不进行T的具体,那么T指int型

//………………………………
template <typename T = int>
T additioned(T a,T b){
return a+b;
}
//………………………………
int main(){
int a=1,b=9;
cout<<additioned<>(a,b);//没有进行T的确定,默认为int
}

模板实例化

格式:

函数模板<实例类型>(参数列表)
类模板<实例类型>

例如:

template <typename T>
T add(T a, T b){
}
template <typename N>
class student{
public:
   N a;
}
//………………………………………………………………
add<float>(1.03,1.2);
student<int> stu;

类模板实例

几个注意

点击看原文
1、模板参数在实体化的时候不能自动类型转换,只有非模板函数才可以

例如:

int max(int,int);

template <class T>
T max(T,T);

在调用的时候:

max('a'42)只会调用非模板函数。
max<>(1,3)用于指定调用模板函数。
max<>2,3.1)调用模板函数,但是会出错,因为模板函数不支持将形参从float转换为int

2、模板函数和非模板函数可以重载
3、如果模板在实际调用的时候得到更好的匹配,则用模板

max(4.0,2.0)max('a','b')调用模板max
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Certainly! Here's an example implementation of the `Vector` class template in C++: ```cpp #include <iostream> #include <vector> template <typename T> class Vector { public: Vector(std::size_t size) : data_(size) {} T& operator[](std::size_t idx) { return data_[idx]; } const T& operator[](std::size_t idx) const { return data_[idx]; } friend std::istream& operator>>(std::istream& in, Vector<T>& v) { for (std::size_t i = 0; i < v.size(); ++i) { in >> v[i]; } return in; } friend std::ostream& operator<<(std::ostream& out, const Vector<T>& v) { out << "[ "; for (std::size_t i = 0; i < v.size(); ++i) { out << v[i] << " "; } out << "]"; return out; } std::size_t size() const { return data_.size(); } private: std::vector<T> data_; }; ``` This implementation defines a `Vector` class template that takes a single template parameter `T` representing the element type of the vector. The class contains a constructor that takes a `std::size_t` parameter representing the size of the vector, and a subscript operator `operator[]` that allows access to individual elements of the vector. The class also defines two friend functions `operator>>` and `operator<<` for input and output, respectively. These functions allow vectors to be streamed to and from `std::istream` and `std::ostream` objects, making it easy to read and write vectors to files or the console. Finally, the class provides a `size()` function that returns the size of the vector. Here's an example of how you could use this class to create and manipulate a vector of `int` elements: ```cpp int main() { Vector<int> v(5); // Create a vector of 5 ints std::cin >> v; // Read values into the vector std::cout << v; // Print the vector return 0; } ``` In this example, the program creates a `Vector` object `v` containing 5 `int` elements, reads values into the vector using the overloaded `operator>>`, and then prints the vector to the console using the overloaded `operator<<`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值