第六章 C++模板

简介:
//------------------------第六章 模板----------------------------------------------
/*
  模板是实现代码重用机制的一种工具,可以实现类型参数化。模板分为函数模板和类模板。
  C++中不建议使用宏,因为宏避开了类型检查机制,容易造成不必要的错误。
  模板声明形式:
  template <class Type> //class可以换成typename
  返回类型 函数名(模板参数表)
  {
      函数体
  }
*/
#include <iostream>
#include <cstring>
using namespace std;

template <typename Type>
Type GetMax(Type lhs, Type rhs)
{
    return lhs > rhs ? lhs : rhs;
}

int main()
{
    int nLhs = 10, nRhs = 90;
    float fLhs = 10.2, fRhs = 21.2;
    double dLhs = 2.11, dRhs = 0.123;
    char cLhs = 'b', cRhs = 'a';

    cout << GetMax(nLhs, nRhs) << endl
         << GetMax(fLhs, fRhs) << endl
         << GetMax(dLhs, dRhs) << endl
         << GetMax(cLhs, cRhs) << endl;
    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;

template <typename Type>
Type Sum(Type *pArray, int size = 0)
{
    Type total = 0;

    for (int i = 0; i < size; i++)
        total += pArray[i];

    return total;
}

int main()
{
    int nArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    double dArray[] = {1.1, 2.2, 3.3, 4.4};

    cout << Sum(nArray, 9) << endl;//output 45
    cout << Sum(dArray, 4) << endl;//output 11

    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;

//template <typename T>与函数模板定义语句之间不允许有别的语句
//int i; //这里出错,不允许
//T sum(T in);
//不过可以同时多个类型

template <typename T1, typename T2>
void OutputData(T1 lhs, T2 rhs)
{
    cout << lhs << " " << rhs << endl;
}

int main()
{
    OutputData('a', "b");
    OutputData(1, 2);
    OutputData(1, 1.9);
    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;

template <typename T>
T Max(T lhs, T rhs)
{
    return lhs > rhs ? lhs : rhs;
}
int Max(int lhs, int rhs)//课本上只写int Max(int, int);但不能通过,必须要有定义
{
    return lhs > rhs ? lhs : rhs;
}

int main()
{
    int i = 10;
    char c = 'a';

    Max(i, i);//正确调用
    Max(c, c);//正确调用

    //解决办法可以是再重载一个普通函数,不需要模板函数,并且要
    //同名,加上int Max(int i, char c)就对了
    cout << Max(i, c) << endl;//出错,原来是模板函数不会进行隐式强制类型转换
    cout << Max(c, i) << endl;//出错,原来是模板函数不会进行隐式强制类型转换
    return 0;
}
下面会继续更新--------------------------------------------------------------------------------------------------------------------- 


目录
相关文章
|
9天前
|
编译器 C语言 C++
c++的学习之路:19、模板
c++的学习之路:19、模板
30 0
|
23天前
|
存储 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 << " "; }`
21 2
|
24天前
|
编译器 C++
【C++初阶】13. 模板进阶
【C++初阶】13. 模板进阶
25 2
|
29天前
|
C++
C++当类模板遇到static
C++当类模板遇到static
|
29天前
|
算法 C++ 容器
C++中模板函数以及类模板的示例(template)
C++中模板函数以及类模板的示例(template)
|
2月前
|
编译器 C++
C++入门指南:10分钟带你快速了解模板究竟是什么(建议收藏!!)
C++入门指南:10分钟带你快速了解模板究竟是什么(建议收藏!!)
34 0
|
2月前
|
编译器 C++
【C++练级之路】【Lv.11】模板(你真的了解模板特化和分离编译吗?)
【C++练级之路】【Lv.11】模板(你真的了解模板特化和分离编译吗?)
|
19小时前
|
C++
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
|
1天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
2天前
|
运维 Serverless Go
Serverless 应用引擎产品使用之在阿里云函数计算中c++模板,将编译好的C++程序放进去部署如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
9 1