当前位置:网站首页>Extraction of templates and generic programming-02-iterator extraction example of fixed extraction technology
Extraction of templates and generic programming-02-iterator extraction example of fixed extraction technology
2022-07-18 18:56:00 【Hair like snow ty】
stay stl The types of iterators in can be divided into the following 5 Categories:
- Output iterator
struct output_iterator_tag; - Input iterator
struct input_iterator_tag; - Forward iterator
struct forward_iterator_tag; - Bidirectional iterator
struct bidirectional_iterator_tag; - Random access iterator
struct random_access_iterator_tag;
stay stl Different containers in often provide different types of iterators , Such as vector Containers provide random access iterators , and list The container provides a bidirectional iterator . Look at the code below :
void _display_category(random_access_iterator_tag mytag)
{
cout << "random_access_iterator_tag\n";
}
void _display_category(bidirectional_iterator_tag mytag)
{
cout << "bidirectional_iterator_tag\n";
}
void _display_category(forward_iterator_tag mytag)
{
cout << "forward_iterator_tag\n";
}
void _display_category(output_iterator_tag mytag)
{
cout << "output_iterator_tag\n";
}
void _display_category(input_iterator_tag mytag)
{
cout << "input_iterator_tag\n";
}
template<typename T>
void display_category(T iter)
{
typename iterator_traits<T>::iterator_category cagy;
_display_category(cagy);
}
int main()
{
display_category(vector<int>::iterator());
display_category(list<int>::iterator());
system("pause");
return 0;
}
result :
random_access_iterator_tag
bidirectional_iterator_tag
And it turns out that ,vector The iterator type corresponding to the container is random_access_iterator_tag,list The container corresponds to bidirectional_iterator_tag.
Upper acquisition vector,list Iterator type of container , It is achieved by fixing the extraction class template . In order to deepen the use of fixed extraction template , Here we use list,vector For example , Take a look at how these two containers let our programmers get the corresponding iterator types with the help of fixed extraction templates .
class _List_My_iterator
{
public:
using iterator_category = bidirectional_iterator_tag;
};
template<class T>
class list_My
{
public:
using iterator = _List_My_iterator;
};
class _Vector_My_iterator
{
public:
using iterator_category = random_access_iterator_tag;
};
template<class T>
class vector_My
{
public:
using iterator = _Vector_My_iterator;
};
template<typename IterT>
struct iterator_traits_My
{
using iterator_category = typename IterT::iterator_category;
};
template<typename T>
void display_category(T iter)
{
typename iterator_traits_My<T>::iterator_category cagy;
_display_category(cagy);
}
int main()
{
display_category(vector_My<int>::iterator());
display_category(list_My<int>::iterator());
system("pause");
return 0;
}
(1) When executed display_category(vector_My<int>::iterator()); when , The first thing to do is create a _Vector_My_iterator Class object , So at this time display_category() Template parameters corresponding to the function template T To be inferred as _Vector_My_iterator type .
(2) Then enter the display_category(), face typename iterator_traits_My<T>::iterator_category cagy; sentence . This code line is an extractor , because T To be inferred as _Vector_My_iterator, So changing careers is equivalent to iterator_traits_My<_Vector_My_iterator>::iterator_category cagy;
(3) In extractor iterator_traits_My in ,T Type is _Vector_My_iterator, therefore using iterator_category = _Vector_My_iterator::iterator_category;
(4) therefore typename iterator_traits_My<T>::iterator_category cagy; amount to _Vector_My_iterator::iterator_category cagy; That is to say random_access_iterator_tag cagy;
result :
random_access_iterator_tag
bidirectional_iterator_tag
边栏推荐
猜你喜欢

Importerror: DLL load failed: the specified module could not be found.

模板于泛型编程-萃取技术-之通过容器类型获取元素类型范例

每日一题·剑指Offer || 041.滑动窗口的平均值(346一样)·队列
![ERROR: Could not install packages due to an OSError: [ Errno 2] No such file or directory: ***](/img/81/6f9b7d554e6a47e614107c799229d2.png)
ERROR: Could not install packages due to an OSError: [ Errno 2] No such file or directory: ***

Exception handling after MySQL IBD file undelete recovery ---- Xi Fenfei

TCP congestion control details | 6 Active queue management

Flink CEP - 复杂事件处理(Complex Event Processing)

CAN光纤转换器解决泰和安TX3016A消防主机长距离联网问题

双动点相对运动破解

第七次笔记:程序的机器级代码表示
随机推荐
Upupwank teak peel installation SSL certificate Guide
三种“榨干”企业装置服装ERP预算的情况
在服务器上如何启动neo4j
02 review multithreading
2022/7/14 cf训练(思维+构造+差分)
离线安装:如何搭建安全的企业级Harbor服务?内容太过详细。
图的深度优先遍历--最短路径
学习路之PHP--post获取不到请求数据
STM32按键外部中断控制LED流水灯-HAL库
JWT learning
ImportError: DLL load failed: 找不到指定的模块。
Experiment on interconnection VLAN of multi port and single arm router
2022/7/15 每日一题(二分+dp+贪心思维)
Redis 与 Mysql 双写一致性方案解析
图的广度优先遍历
软件测试面试题:简述什么是静态测试、动态测试、黑盒测试、白盒测试、α测试 β测试?
[ CTF ]MISC flag
【C语言刷LeetCode】1432. 改变一个整数能得到的最大差值(M)
Use of thingjs
Hcip experiment (3)