C++STL - 函数模板
template<typename A,typenamen B,typename _C>
A function(B arg){
_C var;
...
}
即用<>中的替换template<>中的内容
#include <iostream>
using namespace std;
template<typename T>
T add (T x, T y) {
return x + y;
}
class Integer {
public:
Integer () : m_var (arg) {}
friend ostream& operator<< (ostream& os,Integer const& i) {
return os << i.m_var;
}
Integer const operator+ (Integer const& rhs) const {
return m_var + rhs.m_var;
}
private:
int m_var;
};
int main (void) {
cout << add<, ) << endl;
cout << add<double> (1.3, 4.6) << endl;
cout << add<string> ("Hello, ", "World !") << endl;
// cout << add<char const*> ("Hello, ","World !") << endl;
cout << add<Integer> (, ) << endl;
;
}
#include <iostream>
#include <typeinfo>
using namespace std;
template<typename T> void foo (T x, T y) {
cout << "foo: " << typeid (x).name () << ' ' << typeid (y).name () << endl;
}
template<typename T> void bar (T const& x, T const& y) {
cout << "bar: " << typeid (x).name () << ' ' << typeid (y).name () << endl;
}
template<typename R, typename T> R hum (T x) {
R y;
cout << "hum: " << typeid (x).name () << ' ' << typeid (y).name () << endl;
return y;
}
void f1 (int x, int y) {}
void f2 (double x, double y) {}
int main (void) {
int a, b;
foo (a, b); // i i
double c, d;
bar (c, d); // d d
], f[];
foo (e, f); // Pc Pc
bar (e, f); // A256_c A256_c
// cout << sizeof (e) << endl; // 256
// char (*p)[256] = &e;
// e[0] = 'C'; // *(e+0) = 'C'
foo ("hello", "tarena"); // PKc PKc
// bar ("hello", "tarena"); // A6_c A7_c
bar<string> ("hello", "tarena"); // Ss Ss
bar (string ("hello"), string ("tarena"));
f1 (a, c); // c: double -> int
f2 (a, c); // a: int -> double
// int i = 1.2;
// cout << i << endl;
// 隐式推断的同时不能隐式转换
// foo (a, c); // c: double -> int, T=int
// a: int -> double, T=double
foo ((double)a, c); // d d
foo (a, (int)c); // i i
foo<double> (a, c); // d d
foo<int> (a, c); // i i
// a = hum (c); // 返回值的类型不参与隐式推断
a = hum<int> (c); // d i
;
}
#include <iostream>
#include <typeinfo>
#include <cstring>
using namespace std;
// 两个任意类型值的最大值
template<typename T>
T const& max (T const& x, T const& y) {
cout << "<1" << typeid (x).name () << '>' << flush;
return x < y ? y : x;
}
// 两个任意类型指针所指向目标的最大值
template<typename T>
T* const& max (T* const& x, T* const& y) {
cout << "<2" << typeid (x).name () << '>' << flush;
return *x < *y ? y : x;
}
// 两个字符指针所指向字符串的最大值
char const* const& max (char const* const& x,char const* const& y) {
cout << "<3" << typeid (x).name () << '>' << flush;
? y : x;
}
/*
char const* max (char const* x,char const* y){
cout << "<3" << typeid (x).name () << '>' << flush;
return strcmp (x, y) < 0 ? y : x;
}
*/
// 三个任意类型值的最大值
template<typename T>
T const& max (T const& x, T const& y,
T const& z) {
cout << "<4" << typeid (x).name () << '>' << flush;
return ::max (::max (x, y), z);
}
/*
// 两个字符指针所指向字符串的最大值
char const* const& max (char const* const& x,char const* const& y) {
cout << "<3" << typeid (x).name () << '>' << flush;
return strcmp (x, y) < 0 ? y : x;
}
*/
int main (void) {
, b = ;
cout << ::max (a, b) << endl;
cout << *::max (&a, &b) << endl;
char const* c = "ab", *d = "abc";
cout << ::max (c, d) << endl;
cout << ::max<> (c, d) << endl;
cout << ::max<char const*> (c, d) << endl;
char const* e = "abcd";
char const* const& f = ::max (c, d, e);
cout << f << endl;
";
::max (g, h, i);
cout << f << endl;
;
}
C++STL - 函数模板的更多相关文章
- STL函数模板(即算法)一览
查找算法 adjacent_find:找出一个串中第一个不符合次序的地方 find,find_if:找出第一个符合条件的元素 find_first_of:在一个串中寻找第一个与另一个串中任意一个元素相 ...
- 仿stl+函数模板
#include<iostream> using namespace std; template<class T> void output(T begin, T end) { ...
- STL标准模板库介绍
1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...
- [Reprint] C++函数模板与类模板实例解析
这篇文章主要介绍了C++函数模板与类模板,需要的朋友可以参考下 本文针对C++函数模板与类模板进行了较为详尽的实例解析,有助于帮助读者加深对C++函数模板与类模板的理解.具体内容如下: 泛型编程( ...
- c++函数模板声明与定义相分离
最近在仿写stl,发现stl源码中将模板的声明与定义写在一起实在很不优雅.自己尝试用“传统”方法,及在.h文件里声明,在.cpp文件里定义,然后在main函数里包含.h头文件,这样会报链接错误.这是因 ...
- c++的函数模板和类模板
函数模板和普通函数区别结论: 函数模板不允许自动类型转化 普通函数能够进行自动类型转换 函数模板和普通函数在一起,调用规则: 1 函数模板可以像普通函数一样被重载 2 C++编译器优先考虑普通函数 3 ...
- 读书笔记 effective c++ Item 45 使用成员函数模板来接受“所有兼容类型”
智能指针的行为像是指针,但是没有提供加的功能.例如,Item 13中解释了如何使用标准auto_ptr和tr1::shared_ptr指针在正确的时间自动删除堆上的资源.STL容器中的迭代器基本上都是 ...
- C++_进阶之函数模板_类模板
C++_进阶之函数模板_类模板 第一部分 前言 c++提供了函数模板(function template.)所谓函数模板,实际上是建立一个通用函数,其函数类型和形参类型不具体制定,用一个虚拟的类型来 ...
- C++—模板(1)模板与函数模板
1.引入 如何编写一个通用加法函数?第一个方法是使用函数重载, 针对每个所需相同行为的不同类型重新实现这个函数.C++的这种编程机制给编程者极大的方便,不需要为功能相似.参数不同的函数选用不同的函数名 ...
随机推荐
- 在ASP.NET MVC 中获取当前URL、controller、action
一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟 ...
- 从零开始,搭建博客系统MVC5+EF6搭建框架(4)下,前后台布局实现、发布博客以及展示。
一.博客系统进度回顾 目前已经完成了,前台展示,以及后台发布的功能,最近都在做这个,其实我在国庆的时候就可以弄完的,但是每天自己弄,突然最后国庆2天,连电脑都不想碰,所以就一直拖着,上一篇写了前端实现 ...
- 基于sticky组件,实现带sticky效果的tab导航和滚动导航
上文提供了一个改进版的sticky组件,并将演示效果应用到了自己的博客.有了类似sticky的这种简单组件,我们就可以在利用它开发更丰富的效果,比如本文要介绍的tab导航和滚动导航.实现简单,演示效果 ...
- TypeError: invalid 'in' operand obj
尝试在程序去访问远程的Web API,它在运行时,出现异常: TypeError: invalid 'in' operand obj 由于从服务器返回的数据是json.当我们需要得到这些数据时,还得需 ...
- C#利用反射+特性实现简单的实体映射数据库操作类
附上源代码: using System; using System.Collections.Generic; using System.Data; using System.Linq; using S ...
- P2P核心业务体系
看似一个简单的充值.提现按钮,背后可是巨多的逻辑和业务处理.
- 小议jQuery插件开发
1.写在前面: 大家都知道PHP专注后台与数据库的交互,前端页面中是js的天下,而jQuery作为使用最广泛,最简单有效的js的框架.深受大家的喜欢. 而js作为一门面向对象的开发语言,它独特的语法和 ...
- MongoDB索引
1.目的 索引就是用来加速查询的.数据库索引与书籍的索引类似:有了索引就不需要翻遍整本书,数据库则可以直接在索引中查找,使得查找速度能提高几个数量级.在索引中找到条目以后,就可以直接跳转到目标文档的位 ...
- 弄一个ajax笔记方便查询-$.ajax()
$.ajax()是所有ajax方法中最底层的方法,所有其他方法都是基于$.ajax()方法的封装.这个方法只有一个参数,传递一个各个功能键值对的对象. $.ajax()方法对象参数表: 参数 类型 说 ...
- 深入浅出node(1) Node简介
这一系列主要是自己在学习深入浅出node.js这本书的学习笔试,部分加入了自己的一些理解 分享给一起学习node的小伙伴 自己还是个初学者 有很多地方理解的不到位 一起交流 一 什么是node 1.1 ...