• 【Cherno的C++视频】Writing iterators in C++


    MyVector.h

    #pragma once
    
    template<typename MyVector>
    class MyVectorIterator
    {
    public:
    	using ValueType = typename MyVector::ValueType;
    	using PointerType = ValueType*;
    	using ReferenceType = ValueType&;
    public:
    	MyVectorIterator(PointerType ptr)
    		:m_Ptr(ptr) {}
    
    	MyVectorIterator& operator++()
    	{
    		m_Ptr++;
    		return *this;
    	}
    
    	MyVectorIterator operator++(int)
    	{
    		MyVectorIterator iterator = *this;
    		++(*this);
    		return iterator;
    	}
    
    	MyVectorIterator& operator--()
    	{
    		m_Ptr--;
    		return *this;
    	}
    
    	MyVectorIterator operator--(int)
    	{
    		MyVectorIterator iterator = *this;
    		--(*this);
    		return iterator;
    	}
    
    	ReferenceType operator[](int index)
    	{
    		return *(m_Ptr + index);
    	}
    
    	PointerType operator->()
    	{
    		return m_Ptr;
    	}
    
    	ReferenceType operator*()
    	{
    		return *m_Ptr;
    	}
    
    	ReferenceType operator*(int)
    	{
    		return *m_Ptr;
    	}
    
    	bool operator==(const MyVectorIterator& other) const
    	{
    		return m_Ptr == other.m_Ptr;
    	}
    
    	bool operator!=(const MyVectorIterator& other) const
    	{
    		return !(*this == other);
    	}
    private:
    	PointerType m_Ptr;
    };
    
    template<typename T>
    class MyVector
    {
    public:
    	using ValueType = T;
    	using Iterator = MyVectorIterator<MyVector<T>>;
    public:
    	MyVector()
    	{
    		//allocate 2 elements
    		ReAlloc(2);
    	}
    
    	void PushBack(const T& value)
    	{
    		if (m_Size >= m_Capacity)
    		{
    			ReAlloc(m_Capacity * 2);//double it.
    		}
    		m_Data[m_Size] = value;
    		m_Size++;
    	}
    	// to avoid data copy
    	void PushBack(T&& value)
    	{
    		if (m_Size >= m_Capacity)
    		{
    			ReAlloc(m_Capacity * 2);
    		}
    		m_Data[m_Size] = std::move(value);
    		m_Size++;
    	}
    
    	size_t Size() const { return m_Size; }
    
    	template<typename...Args>
    	T& EmplaceBack(Args&&... args)
    	{
    		if (m_Size >= m_Capacity)
    		{
    			ReAlloc(m_Capacity * 2);
    		}
    		// new() -> advanced tip of the day, for constructing objects in that place.
    		new(&m_Data[m_Size]) T(std::forward<Args>(args)...);//better than:m_Data[m_Size] = T(std::forward(args)...);
    		return m_Data[m_Size++];
    	}
    
    	void PopBack()
    	{
    		if (m_Size > 0)
    		{
    			m_Size--;
    			m_Data[m_Size].~T();
    		}
    	}
    
    	void Clear()
    	{
    		for (size_t i = 0; i < m_Size; i++)
    		{
    			m_Data[i].~T();
    		}
    		m_Size = 0;
    	}
    
    	const T& operator[] (size_t index) const
    	{
    		//size check for debug mode.
    		if (index >= m_Size)
    		{
    			__debugbreak();
    		}
    		return m_Data[index];
    	}
    
    	T& operator[] (size_t index)
    	{
    		//size check for debug mode.
    		if (index >= m_Size)
    		{
    			__debugbreak();
    		}
    		return m_Data[index];
    	}
    
    	~MyVector()
    	{
    		Clear();
    		std::cout << "class MyVector destroyed!\n";
    		//delete[] m_Data;
    		::operator delete(m_Data, m_Capacity * sizeof(T));//::operator delete won't call any destructors.
    		
    		
    	}
    
    	Iterator begin()
    	{
    		return Iterator(m_Data);
    	}
    
    	Iterator end()
    	{
    		return Iterator(m_Data + m_Size);
    	}
    private:
    	void ReAlloc(size_t newCapacity)
    	{
    		// allocate a new block of memory
    		//T* newBlock = new T[newCapacity];//raw pointer->to access memory as low level as we can.
    		T* newBlock = (T*) ::operator new(newCapacity * sizeof(T));//operator new() won't call any constructor.
    
    		//downsize or upsize
    		if (newCapacity < m_Size)
    		{
    			m_Size = newCapacity;
    		}
    
    		// move old elements into the new memory by using a for loop bc need to be 
    		// hitting the copy constructor of all of these elements.
    		// memcpy() for simple types:int float.
    		for (size_t i = 0; i < m_Size; i++)
    		{
    			//newBlock[i] = std::move(m_Data[i]);
    			new (&newBlock[i]) T(std::move(m_Data[i]));
    		}
    
    		// clear
    		for (size_t i = 0; i < m_Size; i++)
    		{
    			m_Data[i].~T();
    		}
    
    		// delete the old block.
    		::operator delete(m_Data, m_Capacity * sizeof(T));// delete[] m_Data;
    		
    		m_Data = newBlock;
    		m_Capacity = newCapacity;
    	}
    private:
    	T* m_Data = nullptr;
    	size_t m_Size = 0;//the number of element.
    	size_t m_Capacity = 0;//how much memory we have allocated.
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216

    main.cpp

    #include 
    #include 
    #include "MyVector.h"
    
    int main(void)
    {
    	MyVector<int> intValues;
    	intValues.EmplaceBack(1);
    	intValues.EmplaceBack(2);
    	intValues.EmplaceBack(3);
    	intValues.EmplaceBack(4);
    	intValues.EmplaceBack(5);
    
    	MyVector<std::string> strValues;
    	strValues.EmplaceBack("1");
    	strValues.EmplaceBack("2");
    	strValues.EmplaceBack("IamGroot");
    	strValues.EmplaceBack("4");
    	strValues.EmplaceBack("5");
    
    	std::cout << "---------Not using iterators:---------\n";
    	for (int i = 0; i < strValues.Size(); i++)
    	{
    		std::cout << strValues[i] << std::endl;
    	}
    	std::cout << "---------range-based for loop:---------\n";
    	for (auto& value : strValues)
    	{
    		std::cout << value << std::endl;
    	}
    	std::cout << "---------iterators:---------\n";
    	for (MyVector<std::string>::Iterator it = strValues.begin(); it != strValues.end(); it++)
    	{
    		std::cout << *it << std::endl;
    	}
    
    	std::cin.get();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    NodeJs 实践之他说
    API 网关的功能
    新华社《中国扫描十年发展图鉴》:扫描全能王为3亿用户带去“掌心里的便利”
    HashMap在JDK1.7中多线程并发会出现死循环,超详细图解
    分布式BASE理论
    vuex01
    java-net-php-python-springboot电商后台管理系统查重PPT计算机毕业设计程序
    责任链模式与spring容器的搭配应用
    【山东科技大学OJ】2413 Problem C: 逆序输出
    解决Antd 二次封装表格的可编辑功能(editable table)不生效的问题
  • 原文地址:https://blog.csdn.net/AlexiaDong/article/details/126574319