STL01——手写简单版本的vector

STL01——手写简单版本的vector


设计一个名为 MyVector 的 Vector 类,该类应具备以下功能和特性:

1、基础成员函数:

构造函数:初始化 Vector 实例
析构函数:清理资源,确保无内存泄露
拷贝构造函数:允许通过现有的 MyVector 实例来创建一个新实例
拷贝赋值操作符:实现 MyVector 实例之间的赋值功能
2、核心功能:

添加元素到末尾:允许在 Vector 的末尾添加新元素
获取元素个数:返回 Vector 当前包含的元素数量
获取容量:返回 Vector 可以容纳的元素总数
访问指定索引处的元素:通过索引访问特定位置的元素
在指定位置插入元素:在 Vector 的特定位置插入一个新元素
删除数组末尾元素:移除 Vector 末尾的元素
清空数组:删除 Vector 中的所有元素,重置其状态
3、迭代与遍历:

使用迭代器遍历:实现迭代器以支持对 Vector 从开始位置到结束位置的遍历
遍历并打印数组元素:提供一个函数,通过迭代器遍历并打印出所有元素
4、高级特性:

容器扩容:当前容量不足以容纳更多元素时,自动扩展 Vector 的容量以存储更多元素
输入描述
题目的包含多行输入,第一行为正整数 N, 代表后续有 N 行命令序列。

接下来 N 行,每行包含一个命令,命令格式为 [operation] [parameters] ,具体命令如下:

push 命令:

格式:push [element]
功能:将 element 添加到 Vector 的末尾
size 命令:

格式:size
功能:返回 Vector 当前包含的元素数量
get 命令:
格式:get [index]
功能:返回 index 索引处的元素
insert 命令:

格式:insert [index] [element]
功能:在 Vector 的 index 索引处插入一个 element
pop 命令:

格式:pop
功能:移除 Vector 末尾的元素
clear 命令:

格式:clear
功能:删除 Vector 中的所有元素,重置其状态
print 命令:

格式:print
功能:提供一个函数,遍历并打印出所有元素
iterator 命令:

格式:iterator
功能:实现迭代器以支持对 Vector 从开始位置到结束位置的遍历
foreach 命令:

格式:foreach
功能:遍历并打印出所有元素
输出描述
题目包含 N 行输出,不同命令需要给出明确的反馈,输入格式如下:

push 命令:无输出

size 命令:输出一个整数,独占一行,代表当前 vector 中的元素数量

get 命令:输出一个整数,独占一行,如果索引有效,则输出指定索引处的元素,如果索引无效,则输出 -1

insert 命令:无输出

pop 命令:无输出

clear 命令:无输出

print 命令:按照顺序打印当前 vector 包含的所有元素,每个元素后都跟一个空格,打印结果独占一行;如果当前的 vector 为空,则打印 empty

iterator 命令:按照顺序打印当前 vector 包含的所有元素,每个元素后都跟一个空格,打印结果独占一行;如果当前的 vector 为空,则打印 empty

foreach 命令:按照顺序打印当前 vector 包含的所有元素,每个元素后都跟一个空格,打印结果独占一行;如果当前的 vector 为空,则打印 empty

#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <stdexcept>
using namespace std;

template<typename T>
class MyVector
{
private:
	T* elements;
	size_t capacity;
	size_t size;
public:
	MyVector()
	{
		elements = nullptr;//什么时候申请空间?
		capacity = 0;
		size = 0;
	}
	~MyVector()
	{
		delete[] elements;
	}
	MyVector(const MyVector& vector)
	{
		size = vector.size;
		capacity = vector.capacity;
		elements = new T[capacity];
		copy(vector.elements, vector.elements + size, elements);
	}
	MyVector& operator=(const MyVector& vector)
	{
		if (*this != vector)//剪枝
		{
			size = vector.size;
			capacity = vector.capacity;
			delete[] elements;//与拷贝构造函数不同之处是此时类已经申请了空间,使用需要释放旧空间,建立新空间。
			elements = new T[capacity];
			copy(vector.elements, vector.elements + size, elements);
		}
		return *this;
	}
	void reserve(size_t  newCapacity)//引用的作用
	{
		if (newCapacity > capacity)
		{
			T* newElement = new T[newCapacity];
			copy(elements, elements + size, newElement);
			delete[] elements;//释放了指针的空间,但是指针本身依然存在
			elements = newElement;
			capacity=newCapacity;
		}
	}
	void push(const T& val)
	{
		if (size == capacity)
			reserve(capacity == 0 ? 1 : 2 * capacity);
		elements[size] = val;
		size++;
	}
	size_t getSize()
	{
		return size;
	}
	size_t getCapacity()
	{
		return capacity;
	}
	T get(size_t index)
	{
		if (index >= size)
			throw out_of_range("index out of range");
		return elements[index];
	}
	void insert(size_t index,const T& val)
	{
		if (index > size)
			throw out_of_range("index out of range");
		if (size == capacity)
			reserve(capacity == 0 ? 1 : 2 * capacity);
		for (size_t i = size; i > index; i--)
		{
			elements[i] = elements[i - 1];
		}
		elements[index] = val;
		size++;
	}
	void pop()
	{
		if (size > 0)
			size--;
	}
	void clear()
	{
		size = 0;
	}
	T* begin()
	{
		return elements;
	}
	T* end()
	{
		return elements + size;
	}
	void print()
	{
		if (size == 0)
			cout << "empty" << endl;
		else{
		for (size_t i = 0; i < size; i++)
		{
			cout << elements[i] << " ";
		}
		cout<<endl;
		}
	}
	void iterator()
	{
		print();
	}
	void foreach()
	{
		print();
	}
	
};

int main() {
	int N;
	std::cin >> N;

	MyVector<int> myVector;

	for (int i = 0; i < N; ++i) {
		std::string command;
		std::cin >> command;

		if (command == "push") {
			int element;
			std::cin >> element;
			myVector.push(element);
		}
		else if (command == "size") {
			std::cout << myVector.getSize() << std::endl;
		}
		else if (command == "get") {
			int index;
			std::cin >> index;
			std::cout << myVector.get(index) << std::endl;
		}
		else if (command == "insert") {
			int index, element;
			std::cin >> index >> element;
			myVector.insert(index, element);
		}
		else if (command == "pop") {
			myVector.pop();
		}
		else if (command == "clear") {
			myVector.clear();
		}
		else if (command == "print") {
			myVector.print();
		}
		else if (command == "iterator") {
			myVector.iterator();
		}
		else if (command == "foreach") {
			myVector.foreach();
		}
		else {
			std::cerr << "Unknown command" << std::endl;
		}
	}

	return 0;
}
  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值