c++官方文档-模版类
#include <iostream>
using namespace std; template<class T>
class MyPair
{
private:
T t[];
T a, b;
public:
MyPair(T t1, T t2)
: a(t1), b(t2)
{
t[] = t1;
t[] = t2;
}
const T* getT()
{
return t;
}
T getMax();
};
/**
* 第一个T指定模版参数
* 第二个T指定函数返回的类型
* 第三个T指定函数的模版参数是类的模版参数
*
*/
template<class T> T MyPair<T>::getMax()
{
//不知道retval的具体类型,所以返回一个临时变量
T retval;
retval = a > b ? a : b;
return retval;
}
template<class R> R f(const R& t)
{
R r = t;
//错误
// t= r;
return r;
} //为模版定义不同的实现
template<class T>
class mycontainer
{
T element;
public:
mycontainer(T args)
{
this->element = args;
}
T increase()
{
return ++element;
}
}; //class template specialization
//模版实例化
//This is because all types
//are known and no template arguments are required for this specialization
//这个是因为所有类型都已经知道而且实例化并不需要模版参数
template<>
class mycontainer<char>
{
char element;
public:
mycontainer(char arg)
{
element = arg;
}
char uppercase()
{
if((element >= 'a') && (element <= 'z'))
element += 'A' - 'a';
return element;
}
};
//template <class T> class mycontainer { ... };//The first line is the generic template
//template <> class mycontainer <char> { ... };//and the second one is the specialization int main()
{
//注释这句话会导致上面那个函数没有编译,能编译通过
f<int>();
MyPair<int> myint(, );
//非常量指针指向常量空间
const int* t = myint.getT();
// t[0]=2000;//invalid
cout << t[] << endl;
cout << myint.getT()[] << endl;
int tt[] = { , };
t = tt;
cout << t[] << endl;
cout << myint.getT()[] << endl;
MyPair<double> mydouble(3.0, 4.0);
cout << mydouble.getMax() << endl; mycontainer<int> myintcontainer();
mycontainer<char> mychar('j');
cout << myintcontainer.increase() << endl;
cout << mychar.uppercase() << endl;
return ;
}
模版函数和模版类
#include <iostream>
using namespace std; class Example4
{
string* ptr;
public:
// constructors:
Example4()
: ptr(new string)
{
}
Example4(const string& str)
: ptr(new string(str))
{
}
// destructor:
~Example4()
{
cout << "调用析构函数" << endl;
delete ptr;
}
// access content:
const string& content() const
{
return *ptr;
}
}; template<class T>
class myClass
{
T t;
public:
myClass(T t)
: t(t)
{ }
template<class R> T f(R const& r, T const& t);
};
template<class T> template<class R> T myClass<T>::f(R const& r, T const& t)
{
cout << "r=" << r << endl;
return t;
} int main()
{
//The destructor for an object is called at the end of its lifetime;
//in the case of foo and bar this happens at the end of function main.
//析构函数在对象生命周期结束后被调用,在这个列子中,foo和bar在main函数的结束时结束
//可以理解成从栈中弹出么
Example4 foo;
Example4 bar("Example");
Example4* fp = new Example4("Example pointer");
cout << "bar's content: " << bar.content() << '\n';
cout << "fp's content: " << fp->content() << '\n';
//指针要手动调用,new 出来的对象,内存块在堆上,不在当前函数栈里
//可以把下面这句话注释了看下
delete fp; myClass<int> myint();
int i = myint.f<double>(200.01, );
cout << i << endl;
return ;
}
下面这个模式就有点奇怪了
#include <iostream>
using namespace std; class Example4
{
string* ptr;
public:
// constructors:
Example4()
: ptr(new string)
{
}
Example4(const string& str)
: ptr(new string(str))
{
}
// destructor:
~Example4()
{
cout << "调用析构函数" << endl;
delete ptr;
}
// access content:
const string& content() const
{
return *ptr;
}
}; template<class T>
class myClass
{
T t;
public:
myClass(T t)
: t(t)
{ }
template<class R> R f(R const& r, R const& t);
};
template<class T> template<class R> R myClass<T>::f(R const& r, R const& t)
{
cout << "r=" << r << endl;
return t;
} int main()
{
//The destructor for an object is called at the end of its lifetime;
//in the case of foo and bar this happens at the end of function main.
//析构函数在对象生命周期结束后被调用,在这个列子中,foo和bar在main函数的结束时结束
//可以理解成从栈中弹出么
Example4 foo;
Example4 bar("Example");
Example4* fp = new Example4("Example pointer");
cout << "bar's content: " << bar.content() << '\n';
cout << "fp's content: " << fp->content() << '\n';
//指针要手动调用,new 出来的对象,内存块在堆上,不在当前函数栈里
//可以把下面这句话注释了看下
delete fp; myClass<int> myint();
cout << myint.f<double>(100.0, 100.0)<<endl;
return ;
}
c++官方文档-模版类的更多相关文章
- Guava官方文档-RateLimiter类
转载自并发编程网 – ifeve.com RateLimiter 从概念上来讲,速率限制器会在可配置的速率下分配许可证.如果必要的话,每个acquire() 会阻塞当前线程直到许可证可用后获取该许可证 ...
- c++官方文档-模版函数和重载
#include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...
- python附录-builtins.py模块str类源码(含str官方文档链接)
python附录-builtins.py模块str类源码 str官方文档链接:https://docs.python.org/3/library/stdtypes.html#text-sequence ...
- Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)
Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...
- Django 2.0官方文档中文 总索引
Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本: ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(中)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(下)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- Kotlin开发语言文档(官方文档)-- 目录
开始阅读Kotlin官方文档.先上文档目录.有些内容还未阅读,有些目录标目翻译还需琢磨琢磨.后续再将具体内容的链接逐步加上. 文档链接:https://kotlinlang.org/docs/kotl ...
随机推荐
- URL的应用
1.对于Android来说,开发应用都会去访问服务器地址,那么就要连网,需要通过URL. 先new一个URL来获取路径,然后利用HttpURLConnection来连接并打开url,并通过get 请求 ...
- JavaScript 之call , apply 和prototype 介绍
1. 前言 为什么将这三个概念放在一起说.原因是这些是会在实现js 继承会需要使用到的 2. call 和 apply call 和 apply 的作用基本类似, 都是去执行function并将这个f ...
- Redis安装和主要功能简介
Redis安装和主要功能简介 Redis(https://redis.io/), 是一个内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 安装Redis 我很少在开发机中直接装各种数 ...
- (5)subprocess模块(子进程模块)
什么是进程 一个程序运行起来了就是一个进程 但是程序本身不是进程,程序是一对代码而已 所以进程就是一个抽象的概念,就是程序运行起来的一个过程 进程和进程之间是相互独立的,互不影响 如何理解子进程和父进 ...
- CTF-练习平台-WEB之 计算题
四.计算题 打开连接 输入后发现只能输入一个数字,在火狐浏览器中按F12,打开查看器 ,如图所示修改最大长度 输入答案后验证,当当当~~flag出现
- 网络流初步:<最大流>——核心(增广路算法)(模板)
增广路的核心就是引入了反向边,使在进行道路探索选择的时候增加了类似于退路的东西[有一点dp的味道??] 具体操作就是:1.首先使用结构体以及数组链表next[ MAXN ]进行边信息的存储 2.[核心 ...
- 【python】面试常考数据结构算法
这里整理的都是基础的不能再基础的算法,目的就是进行一个回忆,同时作为剑指offer的一个补充~嘿嘿~ 查找算法二分查找# 实现一个二分查找# 输入:一个顺序list# 输出: 待查找的元素的位置def ...
- vi文字处理器
http://blog.csdn.net/wangloveall/article/details/22649331 摘要:vi是类UNIX命令行接口的标准文字处理软件,也是进行shell脚本程序编写与 ...
- day33 python学习 多线程
线程的概念 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合),而线程才是cpu上的执行单位. 三 线程与进程的区别 1 1.线程的创建开销小(无需申请内存空间或者资源),创建线程的 ...
- SocketIOCP
项目地址 : https://github.com/kelin-xycs/SocketIOCP SocketIOCP 一个 用 C# Socket 实现 的 IOCP 这是一个 用 C# Socke ...