C++ Vector 顺序表

本文深入探讨C++中的Vector容器,介绍其作为顺序表的数据结构特性,包括动态内存管理、元素插入与删除、迭代器操作等核心概念,并通过示例代码`Vector.h`和`test.cpp`进行实战演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vector.h

#include<iostream>
#include<assert.h>
using namespace std;

typedef int DataType;

class Vector
{
public:
	Vector()//构造函数
		: first(NULL)
		, finish(NULL)
		, end(NULL)
	{}

	size_t size() const//当前已用容量
	{
		return finish - first;
	}

	size_t capacity() const//最大容量
	{
		return end - first;
	}

	void reverse(size_t n)//预留空间
	{
		if (n > capacity())
		{
			expand(n);
		}
	}

	void resize(size_t n, DataType value)//设置分配空间,并对多开空间赋值
	{
		if (n < size())//缩小
		{
			finish = first + n;
		}
		else
		{
			if (n > capacity())
			{
				expand(n);//扩容

			}
			size_t index = n - size();
			while (--index)
			{
				*finish = value;
				finish++;
			}

		}
	}


	Vector(const Vector& v)//拷贝构造   v1(v2)
						   //为避免浅拷贝,需用深拷贝另开空间
	{
		DataType *tmp = new DataType[v.size()];
		first = tmp;
		finish = first + v.size();
		end = first + v.size();
		memcpy(first, v.first, sizeof(DataType)*v.size());
	}

	//v1=v2=v3;
	Vector& operator=(const Vector& v)//赋值运算符重载
	{
		if (this != &v)//传统写法
		{
			delete[] first;
			DataType *tmp = new DataType[sizeof(DataType)*v.size()];
			first = tmp;
			finish = first + v.size();
			end = first + v.capacity();
			memcpy(first, v.first, sizeof(DataType)*v.size());
		}

		//Vector tmp(v);//现代写法
		//swap(first, tmp.first);
		//swap(finish, tmp.finish);
		//swap(end, tmp.end);

		return *this;
	}

	int& operator[](size_t pos)
	{
		return first[pos];
	}

	~Vector()//析构函数
	{
		if (first)
		{
			delete[] first;
			first = finish = end = NULL;
		}

	}


	void Pushback(DataType value)//尾插
	{
		if (finish == end)
		{
			size_t newcapacity = capacity() > 0 ? (2 * capacity()) : 5;
			expand(newcapacity);
		}
		*finish = value;
		finish++;
	}

	void Popback()//尾删
	{
		assert(finish > first);
		finish--;
	}


	void Insert(size_t pos, DataType value)//任意位置插入
	{
		assert(pos >= 0 && pos < size());
		int size = (int)(this->size());
		if (finish == end)
		{
			expand(2 * capacity());
		}
		for (int i = size - 1;i >= (int)pos;)
		{
			first[i + 1] = first[i];
			i--;
		}
		first[pos] = value;
		finish++;
	}


	void Erase(size_t pos)//任意位置删除
	{
		assert(pos >= 0 && pos < size());
		assert(finish > first);
		int size = (int)this->size();
		for (int i = (int)pos;i < size;i++)
		{
			first[i] = first[i + 1];
		}
		finish--;
	}


	DataType* find(DataType value)//查找
	{
		for (size_t i = 0;i < size();i++)
		{
			if (first[i] == value)
			{
				return &(first[i]);
			}
		}
		cout << "没有找到" << endl;
		return NULL;
	}

private:
	void expand(size_t n)//增容
	{
		DataType *tmp = new DataType[sizeof(DataType)*n];
		memcpy(tmp, first, sizeof(DataType)*size());
		size_t size = finish - first;
		size_t capacity = end - first;
		delete[] first;
		first = tmp;
		finish = first + size;
		end = first + n;
	}
private:
	DataType *first;
	DataType *finish;
	DataType *end;
};

test.cpp

#include"Vector.h"



int main()
{

	Vector v1;
	v1.reverse(5);
	v1.Pushback(1);
	v1.Pushback(2);
	v1.Pushback(3);
	v1.Pushback(4);
	v1.Pushback(5);
	v1.Pushback(6);
	v1.Popback();
	v1.Insert(3, 0);
	v1.Erase(1);

	for (size_t i = 0;i < v1.size();i++)
	{
		cout << v1[i] << "  ";

	}
	cout << endl;
	
	cout << *(v1.find(3)) << endl;

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值