博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
In a nutshell: Tags are for overloading, for optimization.
阅读量:6246 次
发布时间:2019-06-22

本文共 1327 字,大约阅读时间需要 4 分钟。

Take a simple advance as example, you might design:

template
void advance(II& i, D n){ while( n-- ) ++i;}

However it have O(n) complexity, which is not acceptable when you have a random_access_iterator. So you may change your design like this:

template
void advance_II(II& i, D n){ while( n-- ) ++i;}template
void advance_RAI(RAI& i, D n){ i += n;}template
void advance(II& i, D n){ if(is_random_access_iterator(i)) // not yet designed advance_RAI(i, n); else advance_II(i, n);}

However the version of function to use is decided at run-time, so we try to let compiler decided which method to choose at compile-time. So we give iterators tags. There are five tags:

struct input_iterator_tag {};struct output_iterator_tag {};struct forward_iterator_tag : public input_iterator_tag {};struct bidirection_iterator_tag : public forward_iterator_tag {};struct random_access_iterator_tag : public bidirection_iterator_tag {};

Now you can do this:

template
void __advance(II& i, D n, input_iterator_tag){ while( n-- ) ++i;}template
void __advance(RAI& i, D n, random_access_iterator_tag){ i += n;}template
void advance(II& i, D n){ __advance(i, n, iterator_traits
::iterator_category());}

图片描述

转载地址:http://jjlia.baihongyu.com/

你可能感兴趣的文章
python html parse
查看>>
本机连接调试Erlang结点与rebar3编译
查看>>
web基础html元素制作web
查看>>
Codeforces 96C - Hockey
查看>>
生成树协议
查看>>
Web应用三种部署方式的优缺点
查看>>
python爬虫——绕开杂乱无章的代码和堵住请求的302异常(2)
查看>>
static易错点
查看>>
js获取当前日期(年月日格式)
查看>>
LeetCode【217. Contains Duplicate】
查看>>
EBook
查看>>
单词加密
查看>>
【转】关于使用GUID和Identity做主键的一些思考
查看>>
oracle入坑日记<六>自增列创建和清除(含序列和触发器的基础用法)
查看>>
JS框架设计之主流框架的引入机制DomeReady一种子模块
查看>>
js失效的原因及解决方式
查看>>
heap堆内存不足
查看>>
scp命令
查看>>
02-Java中的对象和类
查看>>
if 判断语句
查看>>