当前位置:网站首页>Iterator iterator interface
Iterator iterator interface
2022-07-28 14:22:00 【Today is also a day to study hard】
STL iterator iterator Interface
The principle of this interface is to implement it first const_iterator Then derived iterator, And use static type conversion to remove the return value of the function const attribute , The idea comes from VSC++ Standard implementation of .
among const_iterator The following functions are missing definitions , You have to implement it yourself , The definition of other functions is to call the following functions
my_const_iterator() ;// Lack of
my_const_iterator& operator++();// Lack of
my_const_iterator& operator--();// Lack of
reference operator*()const;// Lack of
pointer operator->()const;// Lack of
my_const_iterator& operator+=(const difference_type i);// Lack of
[[nodiscard]] difference_type operator-(const my_const_iterator& other)const;// Lack of
bool operator==(const my_const_iterator& other)const;// Lack of
bool operator<(const my_const_iterator& other)const;// Lack of
Bidirectional iterators and lower iterators need to be modified const_iterator And delete some functions
using iterator_category = std::random_access_iterator_tag;// Iterator type
Complete code :
#include<iterator>
template<typename Type>
class my_const_iterator // Container of const iterator
{
public:// Type the alias
using iterator_category = std::random_access_iterator_tag;// Iterator type
using value_type = Type;
using difference_type = std::ptrdiff_t;// Iterator distance type , By default std::ptrdiff_t;
using pointer = value_type const*;
using reference = const value_type&;
public:
// Construction and destructor
my_const_iterator() ;// Lack of
~my_const_iterator() = default;
// Bidirectional iterator interface
my_const_iterator& operator++();// Lack of
my_const_iterator operator++(int){
my_const_iterator tmp = *this;++*this;return tmp;}
my_const_iterator& operator--();// Lack of
my_const_iterator operator--(int) {
my_const_iterator tmp = *this; --*this; return tmp; }
reference operator*()const;// Lack of
pointer operator->()const;// Lack of
my_const_iterator& operator=(const my_const_iterator& other) = default;
// Random access iterator interface
my_const_iterator& operator+=(const difference_type i);// Lack of
my_const_iterator& operator-=(const difference_type i) {
return *this +=-i}
[[nodiscard]] my_const_iterator operator+(const difference_type i)const {
my_const_iterator tmp = *this;return tmp+=i; }
[[nodiscard]] my_const_iterator operator-(const difference_type i)const {
my_const_iterator tmp = *this; return tmp-= i; }
[[nodiscard]] difference_type operator-(const my_const_iterator& other)const;// Lack of
reference operator[](const difference_type i)const {
return *(*this + i); }
// Comparison function
bool operator==(const my_const_iterator& other)const;// Lack of
bool operator!=(const my_const_iterator& other)const {
return !(*this == other); }
bool operator<(const my_const_iterator& other)const;// Lack of
bool operator<=(const my_const_iterator& other)const {
return !(*this > other); }
bool operator>(const my_const_iterator& other)const {
return other > *this };
bool operator>=(const my_const_iterator& other)const {
return !(*this < other); }
};
template<typename Type>
class my_iterator :public my_const_iterator<Type> {
public:// Type the alias
using BaseIterator = my_const_iterator<Type>;
using iterator_category = BaseIterator::iterator_category;// Iterator type
using value_type = Type;
using difference_type = typename BaseIterator::difference_type;
using pointer = value_type*;
using reference = value_type&;
public:
BaseIterator::BaseIterator;// Inheritance constructor
~my_iterator() = default;
my_iterator& operator=(const my_iterator& other) = default;
// Bidirectional iterator interface
my_iterator& operator++() {
BaseIterator::operator++(); return *this }
my_iterator operator++(int) {
my_iterator tmp = *this; BaseIterator::operator++(); return tmp; }
my_iterator& operator--() {
BaseIterator::operator--(); return *this }
my_iterator operator--(int) {
my_iterator tmp = *this; BaseIterator::operator--(); return tmp; }
reference operator*()const {
return const_cast<reference>(BaseIterator::operator*()); }
pointer operator->()const {
return const_cast<pointer>(BaseIterator::operator->()); }
// Random access iterator interface
my_iterator& operator+=(const difference_type i) {
BaseIterator::operator+=(i); return this }
my_iterator& operator-=(const difference_type i) {
BaseIterator::operator-=(i); return this }
[[nodiscard]] my_iterator operator+(const difference_type i)const {
my_iterator tmp = *this; return tmp += i; }
[[nodiscard]] my_iterator operator-(const difference_type i)const {
my_iterator tmp = *this; return tmp -= i; }
using BaseIterator::operator-;
reference operator[](const difference_type i)const {
return const_cast<reference>( BaseIterator::operator[](i)); }// Will cover all oprator-, So we need to using the other one oprator- Make it visible to the outside
// The comparison function inherits from the base class
};
// Two inverse iterators
template<typename Type>
using my_const_reverse_iterator = std::reverse_iterator<my_const_iterator<Type>>;
template<typename Type>
using my_reverse_iterator = std::reverse_iterator<my_iterator<Type>>;
Limited level. If there is any error, please correct it immediately
边栏推荐
猜你喜欢

Clickhouse distributed cluster construction

阿里、京东、抖音:把云推向产业心脏

UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing

2022年熔化焊接与热切割考题及在线模拟考试

多级缓存方案

2022年熔化焊接与热切割考题及在线模拟考试

目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
![[ecmascript6] async and await](/img/3c/c7de42ad572dc95b188243c02dd228.png)
[ecmascript6] async and await
![[lvgl events] Application of events on different components (I)](/img/a8/7c24e68f3506bbef3c2e922729471c.png)
[lvgl events] Application of events on different components (I)
随机推荐
[util] redis tool class: change the value serializer of redis to genericjackson2jsonredisserializer, and the return value can be object or collection
Forage QR code -- online QR code generator
How to configure ADB environment variables (where to open environment variables)
Install mysql5.7.36 in CentOS
UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing
【Utils】CookieUtil
Nport serial server configuration website (whether the serial server is from network port to serial port)
2022年熔化焊接与热切割考题及在线模拟考试
LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
Read how to deploy highly available k3s with external database
MVC模型:日历系统
How to effectively conduct the review meeting (Part 1)?
QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样
How does vos3000 send incoming calls to okcc
bgp实验
如何有效进行回顾会议(上)?
[ecmascript6] proxy and reflection
Node文件操作
Langjing Technology (Trax China) "robot +ai" opens the era of Chinese retail meta universe
Minitest -- applet automation testing framework