C++ 模板学习 函数模板、类模板、迭代器模板

简介: 使用模板能够极大到使得代码可重用。记录一下,方便后续使用。1. 函数模板,支持多种类型参数 1 #include 2 #include 3 4 //函数模板 5 template 6 T add(T a, T b){ 7 return a + b; 8 } ...

使用模板能够极大到使得代码可重用。

记录一下,方便后续使用。

1. 函数模板,支持多种类型参数

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 //函数模板
 5 template <class T>
 6 T add(T a, T b){ 
 7     return a + b;
 8 }
 9 
10 //函数模板特殊化
11 template <>
12 double add<double>(double a, double b){ 
13     return floor(a + b); 
14 }
15 
16 class Vector{
17 public:
18     Vector(int a = 0, int b = 0):_a(a), _b(b) { } 
19     Vector operator +(Vector &v1){  //重载+
20         Vector res;
21         res._a = this->_a + v1._a;
22         res._b = this->_b + v1._b;
23         return res;
24     }   
25 
26     int _a; 
27     int _b; 
28 };
29 
30 int main (){ 
31     printf("3 + 4 = %d\n", add(3, 4));  //支持int
32     printf("5.6 + 3.7 = %.0lf\n", add(5.6, 3.7));   //支持double
33 
34     Vector v1(1,2);
35     Vector v2(3,4);
36     Vector v3 = add(v1, v2);    //支持类
37     printf("v3(%d, %d)\n", v3._a, v3._b);
38     return 0;
39 }

2. 迭代器模板,支持多种容器

 1 #include <iostream>
 2 #include <vector>
 3 #include <list>
 4 
 5 //该模板函数, 支持各种容器到数据打印
 6 template <class iterator>
 7 void print(iterator begin, iterator end){
 8     for(iterator it = begin; it != end; ++it){
 9         std::cout << *it << std::endl;
10     }   
11 }
12 
13 //使用迭代器需要添加关键字typename
14 //该模板函数, 支持各种容器到数据打印
15 template <class container>
16 void print(container con){
17     for(typename container::iterator it = con.begin(); it != con.end(); ++it){
18         std::cout << *it << std::endl;
19     }   
20 }
21 
22 int main (){ 
23     std::vector<int> my_ver;
24     my_ver.push_back(1);
25     my_ver.push_back(2);
26     my_ver.push_back(3);
27     print(my_ver.begin(), my_ver.end());
28 
29     std::list<std::string> my_list;
30     my_list.push_back("No.1");
31     my_list.push_back("No.2");
32     my_list.push_back("No.3");
33     print(my_list);
34     return 0;
35 }

3.类模板

test_temple.h

 1 #ifndef MATH_CLASS
 2 #define MATH_CLASS
 3 
 4 template <class T>
 5 class Math{
 6 public:
 7     static T add(T v1, T v2){   //方法在类内声明+实现
 8         return v1 + v2; 
 9     }   
10 
11     static T sub(T v1, T v2);   //方法在类内声明
12 };
13 
14 #endif

test_temple.cpp

 1 #include "test_temple.h"
 2 
 3 #ifndef MATH_CPP
 4 #define MATH_CPP
 5 
 6 template <class T>
 7 T Math<T>::sub(T v1, T v2){
 8     return v1 - v2; 
 9 }
10 
11 #endif

test.h

1 //模板类到声明、实现都必须被编译时包含
2 //1.可以将实现都写到头文件
3 //2.或者同时包含.h , .cpp文件
4 
5 //这里把他们了封装, 用户只需要包含test.h即可
6 #include "test_temple.h"
7 #include "test_temple.cpp"

main.cpp

1 #include <stdio.h>
2 #include "test.h"
3 
4 int main(){
5     printf("3 + 4 = %d\n", Math<int>::add(3, 4));
6     printf("20.8 - 5.1 = %.2lf\n", Math<double>::sub(20.8, 5.1));
7     return 0;
8 }

 

相关文章
|
15天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
17 2
|
18小时前
|
C语言 C++
c++的学习之路:4、入门(3)
c++的学习之路:4、入门(3)
7 0
|
18小时前
|
编译器 C++
c++的学习之路:23、多态(2)
c++的学习之路:23、多态(2)
4 0
|
20小时前
|
Java C++
C++的学习之路:21、继承(2)
C++的学习之路:21、继承(2)
5 0
|
21小时前
|
存储 C++ 容器
c++的学习之路:14、list(1)
c++的学习之路:14、list(1)
7 0
|
21小时前
|
存储 算法 C语言
c++的学习之路:9、STL简介与string(1)
c++的学习之路:9、STL简介与string(1)
3 0
|
21小时前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
6 0
|
22小时前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
4 0
|
11天前
|
编译器 C语言 C++
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
18 0
|
3天前
|
存储 安全 C语言
【C++】string类
【C++】string类

热门文章

最新文章