1、函数模板(参数多态)

  相当于一个函数发生器,参数多态,可以重载。

  普通函数和模板函数的本质区别

  1. 普通函数的调用,可以进行隐式的类型转换;
  2. 函数模板的调用,使用类型参数化,严格按照类型进行匹配,不会进行类型的自动转换;

  一个函数模板可以取代许多具体的函数定义,可以大大减少编程工作量。

#include <iostream>
#include <typeinfo>
using namespace std; template <typename P> //函数模板
void ArrayInput(P array, int num)
{
cout << "输入" << num << "个" << typeid(P).name()
<< "\b" << "型数据" << endl;
for (int j = ; j < num; j++)
cin >> array[j];
}
void main()
{
int number;
float floatArray[];
int intArray[];
number = sizeof(floatArray) / sizeof(float);
ArrayInput(floatArray, number);
number = sizeof(intArray) / sizeof(int);
ArrayInput(intArray, number);
system("pause");
}

2、类模板

  使用类模板来定义栈类,进栈、出栈。

#include <iostream>
#include <typeinfo>
using namespace std; template <class T,int i> //函数模板
class MyStack
{
private:
//栈空间:Buffer[0]~Buffer[i-1],Buffer[i]表示栈底
T Buffer[i + ];
int size;
int top;
public:
MyStack(T zero)
{
size = i;
top = i;
for (int j = ; j <= i; j++) //初始化缓冲区
{
Buffer[j] = zero;
}
}
void push(const T item);
T pop();
}; template <class T,int i> // 模板类成员函数的定义
void MyStack<T, i>::push(const T item)
{
if (top > )
Buffer[--top] = item;
else
cout << "栈溢出" << endl;
} template <class T,int i>
T MyStack<T, i>::pop()
{
T temp;
if (top < size)
temp = Buffer[top++];
else
{
temp = Buffer[top];
cout << "栈已空" << endl;
}
return temp;
} void main()
{
MyStack<int, > S1();
S1.push();
cout << S1.pop() << endl;
MyStack<char*, > S2("empty");
S2.push("china");
cout << S2.pop() << endl;
cout << S2.pop() << endl;
system("pause");
}

C++(四十二) — 函数模板多态的更多相关文章

  1. NeHe OpenGL教程 第四十二课:多重视口

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. (C/C++学习笔记) 二十二. 标准模板库

    二十二. 标准模板库 ● STL基本介绍 标准模板库(STL, standard template library): C++提供的大量的函数模板(通用算法)和类模板. ※ 为什么我们一般不需要自己写 ...

  3. 网站开发进阶(四十二)巧用clear:both

    网站开发进阶(四十二)巧用clear:both 前言 我们在制作网页中用div+css或者称xhtml+css都会遇到一些很诡异的情况,明明布局正确,但是整个画面却混乱起来了,有时候在IE6下看的很正 ...

  4. Gradle 1.12用户指南翻译——第四十二章. Announce插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  5. SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关

    0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ...

  6. “全栈2019”Java第四十二章:静态代码块与初始化顺序

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. 第四十二个知识点:看看你的C代码为蒙哥马利乘法,你能确定它可能在哪里泄漏侧信道路吗?

    第四十二个知识点:看看你的C代码为蒙哥马利乘法,你能确定它可能在哪里泄漏侧信道路吗? 几个月前(回到3月份),您可能还记得我在这个系列的52件东西中发布了第23件(可以在这里找到).这篇文章的标题是& ...

  8. 【C++】模板简述(二):函数模板

    我们上文讲了,模板的引入,我们发现在某种特殊的情况下,必须得通过模板才能完美的解决问题. 本文就来简述一下函数模板的基本使用. 一.函数模板格式 template<typename Param1 ...

  9. abp(net core)+easyui+efcore实现仓储管理系统——入库管理之六(四十二)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

随机推荐

  1. 【LeetCode算法-28/35】Implement strStr()/Search Insert Position

    LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...

  2. postgrelsql 的 wm_concat : string_agg

    string_agg,array_agg 这两个函数的功能大同小异,只不过合并数据的类型不同 array_agg(expression) 把表达式变成一个数组 一般配合 array_to_string ...

  3. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  4. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. jquery向上滚动页面的写法

    jquery向上滚动页面的写法<pre> $('.arrow_top').on('click',function () { $body = (window.opera) ? (docume ...

  6. 如何在Linux中结合示例使用'cat'和'tac'命令

    上一篇我们讲到了cat的使用示例:https://www.cnblogs.com/WeiLian1024/p/11863057.html 本篇我们将继续延续Cat讲讲Tac 本文是我们讲讲Linux技 ...

  7. Python2.7+virtualenv+CUDA 10.0版的pytorch v1.3.0 +运行人群计数crowdcount-mcnn网络

    Python2.7$ python2 -m virtualenv pytorchenv$ source pytorchenv/bin/activate $ pip install ipython py ...

  8. 10 Servlet+Http+Request对象

    1.Servlet的体系结构 Servlet -- 接口----->GenericServlet -- 抽象类------->HttpServlet -- 抽象类 (1)GenericSe ...

  9. Python安装-Pycharm+Anaconda

    1.初识Python Python是一门非常简单优雅的编程语言,可以用极少的代码就能实现强大的功能,而且学习起来十分简单,没有编程基础也可轻松入门.其功能强大,特别是第三方库的库的支持,使得开发方便十 ...

  10. Gym 102055B Balance of the Force

    大意: $n$个骑士, 第$i$个骑士若加入光明阵营, 那么能力值$L_i$, 加入黑暗阵营, 能力值$D_i$. 给定$m$个限制$(u_i,v_i)$, 表示$u_i,v_i$不能在同一阵营. 求 ...