实现一个简单的vector容器

5 篇文章 0 订阅
3 篇文章 0 订阅

当然还有好多没实现,列如二维的数组,里面会用到可变模板参数
当然有空就改进下,自己实现的迭代器还有容器

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;	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值