c++面向对象高级编程 学习十一 类模板、函数模板、成员模板

namespace经验谈:
团队中函数或类的名字可能会冲突,因此使用namespace进行区分。
在这里插入图片描述
类模板:
template<typename T>
在这里插入图片描述
函数模板:
template<class T>,此处class可改成typename
函数模板在使用的时候,不必指明类型,因为函数模板是去调用该函数,会在调用的时候传入参数,编译器会对函数模板进行参数推导,而类模板则需指明类型。
在这里插入图片描述

成员模板:
在一个类中有一个成员,该成员是template,则称该成员为成员模板。
在这里插入图片描述
在这里插入图片描述

template <class U1,class U2>
pair(const pair<U1,U2>&p):
first(p.first),second(p.second){}

该pair类允许任意的T1,T2,也允许任意的U1,U2,但是在编译的过程中必须满足first(p.first),second(p.second)的初始化赋值过程,在该示例中即,鲫鱼,麻雀初始化赋值到first和second中没有问题,而反之则编译不能通过。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446600.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

操作系统面试 总结

以下文章来源于程序员cxuan &#xff0c;作者cxuan 原文链接什么是操作系统 操作系统是管理硬件和软件的一种应用程序。操作系统是运行在计算机上最重要的一种软件&#xff0c;它管理计算机的资源和进程以及所有的硬件和软件。它为计算机硬件和软件提供了一种中间层&#xff…

英语口语week 14 Thursday

英语文章 A couple decided to go out to celebrate their wedding anniversary, so they called a babysitter. When the babysitter arrived, the two children had already been asleep. The babysitter soon got bored and went to the kitchen where she blended some wh…

c++面向对象高级编程 学习十二 模板

模板特化&#xff1a; 模板是一种泛化的形式&#xff0c;特化是将参数类型进行指定&#xff0c;写出特化的版本&#xff0c;当在调用下图cout<<hash()(1000);的时候&#xff0c;由于特化中有struct hash{ }的版本&#xff0c;因此会直接调用特化部分。 模板偏特化&…

英语口语 week14 Friday

英语文章 Shopping is taking place every second. However, the prices of the same goods may differ from store to store. A name-brand dress may cost several hundred pounds at a boutique, but only half the price in a discount store or a big chain store. Moreo…

数据结构 树

定义 树是节点的优先集合度&#xff1a;孩子的数量&#xff0c;度为0 就是终端节点&#xff0c;不为零就是根节点有序树&#xff1a;有顺序&#xff0c;不可以替换无序树&#xff1a;无顺序&#xff0c;可以替换深度 和 树的深度相反&#xff0c;第一层深度为1 树的深度为 3 二…

英语口语 Week15 TuesDay

英语文章 One day, when Bella was doing sports in the school yard, the squirrel fled out of her sleeve. Threading its way through a considerable number of people, the squirrel disappeared in the distance After a sequence of movements, it hopped onto the ar…

c++面向对象高级编程 学习十四 引用

文章目录referencereference的常见用途reference 变量有三种形式&#xff1a;值&#xff0c;指针&#xff0c;引用 int x0; //值 int* p&x;//指向整型的指针&#xff0c;地址&#xff0c;指针在之后的程序中可以指向其他变量 int& rx;//引用&#xff0c;此处表示 r代…

google浏览器 隐藏功能开启

网址 chrome://flags/ 1&#xff0c;多线程下载 2&#xff0c;暗黑模式3&#xff0c;标签缩略图4&#xff0c;PWA 渐进式web应用 网页即应用5&#xff0c;阅读模式&#xff0c;排除广告&#xff0c;点击阅读模式去除干扰chrome://net-internals6&#xff0c;解决有问题的代理IP…

英语口语Week 15 Wednesday

英语文章 Accomplishing the task assigned by the teacher; Julia rushed out. Squatting at the gate and playing with the squirrel, Bella waved at the sight of Julia and yelled out here" . Julia ran quickly towards them, pointed at the squirrel and asked…

c++面向对象高级编程 学习十五 组合继承关系下的构造和析构

文章目录继承关系组合关系继承和组合继承关系 构造由内而外&#xff0c;析构由外而内&#xff0c;内即是父类 组合关系 A拥有B&#xff0c; 构造由内而外&#xff0c;析构由外而内&#xff0c;内即是B 继承和组合 构造和析构顺序如图&#xff1a;

英语口语Week16 Wednesday

英语文章 Recently my friend received a gift from her boyfriend - a very expensive bracelet. But the substance of her response left us in astonishment - she didn’t attend to the exquisiteness(of the gift and wanted to return it to him In terms of salary, …

C++ 查漏补缺

特性关系 C语言面向过程C面向过程 面向对象(封装 继承 多态)C具备C语言的全部特性的基础上&#xff0c;并且支持更多新的特性 内存泄露 申请内存&#xff0c;没有释放申请 malloc new释放 free deleteProcessExplorer查看内存是否释放 代码移植 将生成的exe运行在别的平台&…

c++面向对象高级编程 学习十六 vptr和vtbl

当一个类中有一个或多个虚函数时&#xff0c;内存中会多一个虚指针&#xff08;vptr&#xff0c;virtual pointer&#xff09;&#xff0c;指向一个虚表&#xff08;vtbl&#xff0c;virtual table&#xff09; 父类有虚函数&#xff0c;则子类一定有虚函数 在下图示意图中&a…

英语口语Week16 Thursday

英语文章 It is an impossibility that everything runs smoothly in everyday life. Where there is trouble, there could be anxiety.Anxiety is a common phenomenon; you are not the only one carrying it. But, it could be somewhat poisonous if you don’t let it o…

static内容相关介绍学习

说一下static关键字的作用 当程序执行到函数内部定义的变量时&#xff0c;编译器为它在栈上分配空间&#xff0c;函数在栈上分配的空间在此函数执行结束时会释放掉&#xff0c;这样就产生了一个问题: 如果想将函数中此变量的值保存至下一次调用时&#xff0c;如何实现&#xf…

C++STL与泛型编程(2) 第一个C++ STL Application

文章目录STL六大部件STL六大部件代码示例时间复杂度前闭后开区间auto关键字的用法STL六大部件 容器 分配器 算法 迭代器 适配器 仿函数 容器要放东西&#xff0c;东西要占用内存&#xff0c;分配器可支持容器解决内存问题。算法处理容器中的数据。迭代器是容器和算法之间的桥…

C++STL与泛型编程(3)容器之分类与测试

文章目录容器的分类序列式容器&#xff08;sequence containers&#xff09;代码示例辅助函数array 容器array容器的测试代码测试代码中部分函数解析vector 容器vector 容器的测试代码测试代码中部分函数解析list 容器list 容器的测试代码测试代码中部分函数解析forward_list 容…

C++面试题目

C和C的区别 总览 C是一个结构化语言&#xff0c;它的重点在于算法和数据结构。C程序的设计首要考虑的是如何通过一个过程&#xff0c;对输入&#xff08;或环境条件&#xff09;进行运算处理得到输出&#xff08;或实现过程&#xff08;事务&#xff09;控制&#xff09;。C&…

C++STL与泛型编程(4)OOP(面向对象编程) Vs. GP(泛型编程)

文章目录OOP和GP为什么list不能使用标准库中的::sort算法&#xff1f;采用GP的好处OOP和GP OOP将datas和methods关联在一起 GP将datas和methods分开 为什么list不能使用标准库中的::sort算法&#xff1f; 因为标准库的sort的算法用到了随机访问迭代器&#xff08;RandomAcce…

牛客网 链表结构 算法相关内容

链表结构 单链表的节点结构 由以下结构的节点依次连接起来所形成的链叫单链表结构 Clas Node<V>{ V value; Node next; } 双链表的节点结构 由以下结构的节点依次连接起来所形成的链叫双链表结构 Clas Node<V>{ V value; Node next; Node last; } 单链表和双…