编程语言
首页 > 编程语言> > 【C++】STL中vector和迭代器的int类型数据的仿写

【C++】STL中vector和迭代器的int类型数据的仿写

作者:互联网

文章目录

(一)my_vecrot.h文件

#ifndef  MY_VECTOR_H
#define MY_VECTOR_H
#include <iostream>
using namespace std;

//myvector容器
class myvector
{
public:
	myvector();

	~myvector();

	void insert(int pos, int value);
	void erase(int pos);
	void resize(int num);

	int& operator[](int idx);
	//迭代器类:可以使用该类的对象,做指针的操作
	class myiterator
	{
	public:
		myiterator(myvector* pvc, int idx)
			:vc(pvc),
			index(idx)
		{}
		myiterator operator++(int)
		{
			myiterator tmp(*this);
			index++;
			return tmp;
		}
		bool operator!=(const myiterator& rhs)
		{
			return index != rhs.index;
		}
		int& operator*() //迭代器解引用
		{
			return (*vc)[index];
		}
	private:
		int index;
		myvector* vc;
	};

	myiterator getbegin();
	myiterator getend();
private:

	bool Empty();
	bool Full();
	bool ApplyNewspace();

	int* data_space;	//连续数据空间首地址
	int cur_size;	//当前元素个数
	int capacity;	//容器容量
};


#endif

(二)my_vector.cpp文件

#include "my_vector.h"

//容器实现
myvector::myvector()
{
	data_space = new int[2];
	cur_size = 0;
	capacity = 2;
}
	
myvector::~myvector()
{
	delete[] data_space;
	data_space = NULL;
}

void myvector::insert(int pos, int value)
{
	if (Full() && !ApplyNewspace())
	{
		throw exception("Applynewspace error");
	}
	for (int i = cur_size; i > pos; i--)
	{
		data_space[i] = data_space[i - 1];
	}
	data_space[pos] = value;
	cur_size++;
}
void myvector::erase(int pos)
{
	if (Empty() || pos < 0 || pos > cur_size)
	{
		throw exception("empty vector or erase pos err");
	}

	for (int i = pos; i < cur_size - 1; i++)
	{
		data_space[i] = data_space[i + 1];
	}
	cur_size--;
}
/*
resize(int num); 重新改变容器的元素个数,个数变多num>size(),使用默认值0填充新位置;
若变短num<size() ,超出长度的元素被删除,但是容量不会变
*/
void myvector::resize(int num)
{
	if (num < 0)
	{
		throw exception("num can not -num");
	}
	if (num > capacity)
	{
		int* tmp = new int[num];
		memcpy(tmp, data_space, sizeof(int) * capacity);
		memset(tmp + cur_size, 0, sizeof(int) * (num - cur_size));
		delete[] data_space;
		data_space = tmp;
		capacity = num;
	}
	else if (num <= cur_size)
	{
		cur_size = num;
		memset(data_space + cur_size, 0, sizeof(int) * (capacity - cur_size));
	}
	else
	{
		memset(data_space + cur_size, 0, sizeof(int) * (capacity - cur_size));
	}

}

bool myvector::Empty()
{
	return cur_size == 0;
}
bool myvector::Full()
{
	return cur_size == capacity;
}
//扩容
bool myvector::ApplyNewspace()
{
	int* newspace = new int[2 * capacity];
	memcpy(newspace, data_space, sizeof(int) * capacity);
	delete[] data_space;
	data_space = newspace;
	capacity *= 2;
	return true;
}
//[]运算符重载
int& myvector::operator[](int idx)
{
	//抛出异常
	if (idx < 0 || idx > cur_size)
	{
		throw exception("[idx] out range");
	}
	return data_space[idx];
}

//获取首元素、尾元素下一个地址的迭代器类型
myvector::myiterator myvector::getbegin()
{
	return myiterator(this, 0);
}
myvector::myiterator myvector::getend()
{
	return myiterator(this, cur_size);
}


(三)main.cpp文件

#include "my_vector.h"

void Print_vector(myvector& vc)
{
	myvector::myiterator it = vc.getbegin();
	while (it != vc.getend())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

void Test()
{
	myvector vc;
	for (int i = 0; i < 10; i++)
	{
		vc.insert(i, i * 10);
	}
	Print_vector(vc);

	//使用erase(int pos)

	vc.erase(3);
	Print_vector(vc);

	vc.resize(15);
	Print_vector(vc);
	
	vc.resize(5);
	Print_vector(vc);
}

int main()
{
	Test();
}

(四)简单测试结果

在这里插入图片描述

(五)主要目的

//myvector容器
class myvector
{
public:
	myvector();

	~myvector();

	void insert(int pos, int value);
	void erase(int pos);
	void resize(int num);

	int& operator[](int idx);
	//迭代器类:是一个对象,做指针的操作
	class myiterator
	{
	public:
		myiterator(myvector* pvc, int idx)
			:vc(pvc),
			index(idx)
		{}
		myiterator operator++(int)
		{
			//系统提供浅拷贝
			myiterator tmp(*this); 
			index++;
			return tmp;
		}
		bool operator!=(const myiterator& rhs)
		{
			return index != rhs.index;
		}
		int& operator*() //迭代器解引用
		{
			return (*vc)[index];
		}
	private:
		int index;		//下标寻找位置
		myvector* vc;	
	};

	myiterator getbegin();
	myiterator getend();
private:

	bool Empty();
	bool Full();
	bool ApplyNewspace();

	int* data_space;	//连续数据空间首地址
	int cur_size;	//当前元素个数
	int capacity;	//容器容量
};


标签:myvector,cur,仿写,space,STL,myiterator,C++,int,size
来源: https://blog.csdn.net/xiaoxiaoguailou/article/details/121438063