zoukankan      html  css  js  c++  java
  • C++ 模板学习 函数模板、类模板、迭代器模板

    使用模板能够极大到使得代码可重用。
    记录一下,方便后续使用。
    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
    ", add(3, 4));  //支持int
    32     printf("5.6 + 3.7 = %.0lf
    ", 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)
    ", 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
    ", Math<int>::add(3, 4));
    6     printf("20.8 - 5.1 = %.2lf
    ", Math<double>::sub(20.8, 5.1));
    7     return 0;
    8 }
  • 相关阅读:
    System.Web.HtppRuntime.cs
    Code:Tree
    Code-Helper-Web:CacheHelper.cs
    Code-Helper-Web:CookieHelper.cs
    ORACLE数据库常用查询二
    涂抹Oracle笔记2:数据库的连接-启动-关闭
    windows下常用的操作命令及dos命令
    涂抹Oracle笔记1-创建数据库及配置监听程序
    表空间满处理方法
    SQL内连接-外连接join,left join,right join,full join
  • 原文地址:https://www.cnblogs.com/profession/p/10314981.html
Copyright © 2011-2022 走看看