当然还有好多没实现,列如二维的数组,里面会用到可变模板参数
当然有空就改进下,自己实现的迭代器还有容器
MyVector.h
#pragma once
#ifndef MYVECTOR
#define MYVECTOR
#include <algorithm>
template<class T>
class MyVector;
template<class T>
class MyIterator;
template <class T>
class MyVector {
public:
MyVector<T>(int length = 0, int capacity = 0) : length(length), capacity(capacity) {
ptr = nullptr;
}
MyVector<T>(MyVector<T>&object) {
this->ptr = object.ptr;
this->length = object.length;
this->capacity = object.capacity;
}
friend class MyIterator<T>;
~MyVector<T>() {
if (ptr)
delete[] ptr;
length = 0;
capacity = 0;
}
void push_back(T date) {
CapacitySize();
ptr[length++] = date;
};
void pop_back() {
T* temp = new T[--capacity];
for (int i = 0; i < length-1; i++) {
temp[i] = ptr[i];
}
delete[] ptr;
ptr = temp;
length--;
}
T front() const{
return *ptr;
}
T back() const{
return ptr[length - 1];
}
MyIterator<T>&begin() {
m_begin.ptr = ptr;
return m_begin;
}
MyIterator<T>&end() {
T* temp = ptr;
for (int i = 0; i < length; i++) {
temp++;
}
m_end.ptr = temp;
m_end.index = length;
return m_end;
}
MyIterator<T>& rbegin() {
m_begin.ptr = ptr;
return m_begin;
}
MyIterator<T>& rend() {
end();
std::reverse(m_begin, m_end);
return m_end;
}
T &operator[](int index) const{
if (index > length)
throw "下标非法";
return ptr[index];
}
const int size() {
return length;
}
private:
void CapacitySize() {
T* temp = new T[++capacity];
if (!temp)
throw "new 失败";
for (int i = 0; i < length; i++)
temp[i] = ptr[i];
delete[] ptr;
ptr = temp;
}
T *getPtr() { return ptr; }
private:
T* ptr;
int length;
int capacity;
MyIterator<T>m_begin;
MyIterator<T>m_end;
};
template<class T>
class MyIterator{
public:
MyIterator<T>(int index = 0):index(index) {
ptr = nullptr;
}
~MyIterator<T>() {}
friend class MyVector<T>;
T &operator*() const {
return *this->ptr;
}
MyIterator<T>& operator++(int) { // 后缀
MyIterator<T>temp(*this);
++(*this);
return temp;
}
MyIterator<T>& operator++() { // 前缀
this->ptr++;
index++;
return *this;
}
MyIterator<T>& operator--(int) {
MyIterator<T>temp(*this);
--(*this);
return temp;
}
MyIterator<T>& operator--() {
this->ptr--;
index--;
return *this;
}
bool operator!=(MyIterator<T>&obj) {
if (this->index == obj.index)
return false;
return true;
}
MyIterator<T> &operator+(T obj) {
*this->ptr = *this->ptr+obj;
return *this;
}
MyIterator<T>& operator+=(T obj) {
*this->ptr += obj;
return *this;
}
MyIterator<T>& operator-(T obj) {
*this->ptr = *this->ptr - obj;
return *this;
}
MyIterator<T>& operator-=(T obj) {
*this->ptr -= obj;
return *this;
}
MyIterator<T>& operator+(const T obj) const{
*this->ptr = *this->ptr + obj;
return *this;
}
MyIterator<T>& operator+=(const T obj) const{
*this->ptr += obj;
return *this;
}
private:
T* ptr;
int index;
};
#endif // MYVECTOR
main.cpp
#include <iostream>
#include <string>
#include "MyVector.h"
using namespace std;
int main() {
MyVector<int>s;
s.push_back(1);
s.push_back(2);
s.push_back(3);
for (auto i = s.rbegin(); i != s.rend();) {
i += 1;
cout << *i << "\t";
i++;
}
for (auto i = s.begin(); i != s.end();) {
cout << *i << "\t";
i++;
}
for (auto i : s) {
cout << i << "\t";
}
MyVector<string>str;
str.push_back("hello");
str.push_back("world");
for (auto i = str.begin(); i != str.end();) {
i += "233";
cout << *i << "\t";
i++;
}
while (true);
return 0;
}