STL vector

文章目录

  • [一、vector 类的模拟实现](#一、vector 类的模拟实现)


vector 是一个动态增长的数组,可以存储任意类型

模板参数 T 表示存储元素的类型,Alloc 是空间配置器,一般不用传

vector 的接口使用和 string 类似,参考 string

一、vector 类的模拟实现

vector 类中成员的意义:

  • start:指向动态数组第一个元素
  • finish:指向动态数组最后一个元素的下一个位置
  • end_of_storage:指向动态数组已开辟空间的最后一个空间的下一个位置

vector 类常用接口模拟实现:

cpp 复制代码
//test.cpp
#include "vector.h"

int main()
{
	starrycat::vector_test4();

	return 0;
}

//vector.h
#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <assert.h>
#include <algorithm>

using std::cout;
using std::endl;

namespace starrycat
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return start;
		}

		const_iterator begin() const
		{
			return start;
		}

		iterator end()
		{
			return finish;
		}

		const_iterator end() const
		{
			return finish;
		}

		//默认构造函数
		vector<T>()
		{}

		//n 个 value 构造
		//const 引用会延长匿名对象的生命周期
		vector<T>(size_t n, const T& value = T())
		{
			reserve(n);

			for (int i = 0; i < n; ++i)
			{
				start[i] = value;
			}

			finish = start + n;
		}

		//避免内置类型构造时调用迭代器区间构造
		//当第一个参数为整形家族时,需要重载函数
		vector<T>(int n, const T& value = T())
		{
			reserve(n);

			for (int i = 0; i < n; ++i)
			{
				start[i] = value;
			}

			finish = start + n;
		}

		//迭代器区间构造
		template<class InputIterator>
		vector<T>(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		//拷贝构造
		//vector<T>(const vector<T>& v)
		//{
		//	//开空间
		//	reserve(v.capacity());

		//	//深拷贝 vector 数据
		//	for (int i = 0; i < v.size(); ++i)
		//	{
		//		start[i] = v.start[i];
		//	}

		//	finish = start + v.size();
		//	end_of_storage = start + v.capacity();
		//}

		//现代写法
		vector<T>(const vector<T>& v)
		{
			vector<T> tmp(v.begin(), v.end());
			swap(tmp);
		}

		//赋值重载
		//vector<T>& operator=(const vector<T>& v)
		//{
		//	if (this != &v)
		//	{
		//		T* tmp = new T[v.capacity() == 0 ? 4 : v.capacity()];

		//		//深拷贝数据
		//		for (int i = 0; i < v.size(); ++i)
		//		{
		//			tmp[i] = v.start[i];
		//		}

		//		delete[] start;
		//		start = tmp;
		//		finish = start + v.size();
		//		end_of_storage = start + v.capacity();
		//	}

		//	return *this;
		//}

		//现代写法
		vector<T>& operator=(vector<T> v)
		{
			swap(v);

			return *this;
		}

		void swap(vector<T>& v)
		{
			std::swap(start, v.start);
			std::swap(finish, v.finish);
			std::swap(end_of_storage, v.end_of_storage);
		}

		//析构函数
		~vector<T>()
		{
			delete[] start;
			start = nullptr;
			finish = nullptr;
			end_of_storage = nullptr;
		}

		size_t size() const
		{
			return finish - start;
		}

		size_t capacity() const
		{
			return end_of_storage - start;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());

			return start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());

			return start[pos];
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				//深拷贝
				T* tmp = new T[n];
				
				//vector 中如果是自定义类型,也需要深拷贝
				//需要提前保存 sz,否则释放空间后 finish 位置就不对了
				const size_t sz = size();
				for (int i = 0; i < sz; ++i)
				{
					tmp[i] = start[i];
				}

				delete[] start;
				start = tmp;
				finish = start + sz;
				end_of_storage = start + n;
			}
		}

		void resize(size_t n, T value = T())
		{
			if (n < size())
			{
				finish = start + n;
			}
			else
			{
				if (n > capacity())
				{
					reserve(n);
				}

				while (finish != start + n)
				{
					*finish = value;
					++finish;
				}
			}
		}

		bool empty() const
		{
			return start == finish;
		}

		void push_back(const T& x)
		{
			//扩容
			//if (finish == end_of_storage)
			//{
			//	reserve(capacity() == 0 ? 4 : 2 * capacity());
			//}

			插入数据
			//*finish = x;
			//++finish;

			insert(end(), x);
		}

		void pop_back()
		{
			//assert(!empty());

			//--finish;

			erase(end() - 1);
		}

		iterator insert(iterator pos, const T& value)
		{
			assert(pos <= finish);

			//扩容
			if (finish == end_of_storage)
			{
				//需要更新 pos
				size_t posIndex = pos - start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = start + posIndex;
			}

			//移动数据
			iterator cur = end();
			while (cur != pos)
			{
				*cur = *(cur - 1);
				--cur;
			}

			*pos = value;
			++finish;

			return pos;
		}

		void erase(iterator pos)
		{
			assert(pos < finish);

			iterator cur = pos + 1;
			while (cur != end())
			{
				*(cur - 1) = *cur;
				++cur;
			}

			--finish;
		}
		
	private:
		iterator start = nullptr;
		iterator finish = nullptr;
		iterator end_of_storage = nullptr;
	};

	void Print(const vector<int>& v)
	{
		vector<int>::const_iterator it = v.begin();
		while (it != v.end())
		{
			//(*it) *= 2;
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (int i = 0; i < v.size(); ++i)
		{
			//v[i] *= 2;
			cout << v[i] << " ";
		}
		cout << endl;
	}

	void vector_test1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			(*it) *= 10;
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		for (int i = 0; i < v.size(); ++i)
		{
			v[i] = i;
			cout << v[i] << " ";
		}
		cout << endl;
		
		Print(v);
	}

	void vector_test2()
	{
		//3 个 1 构造
		vector<int> v1(3, 1);
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<int> v2(3);
		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;

		//3 个 "111" 构造
		std::string s1 = "111";
		vector<std::string> v3(3, s1);
		for (const std::string& e : v3)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<std::string> v4(3);
		for (const std::string& e : v4)
		{
			cout << e << " ";
		}
		cout << endl;

		//3 个 v1 构造
		vector<vector<int>> v5(3, v1);
		for (const vector<int>& e1 : v5)
		{
			for (auto e2 : e1)
			{
				cout << e2 << " ";
			}
			cout << endl;
		}
		cout << endl;

		//迭代器区间构造
		std::string s2 = "abcde";
		vector<int> v6(s2.begin(), s2.end());
		for (auto e : v6)
		{
			cout << e << " ";
		}
		cout << endl;

		int arr[] = { 4, 2, 1, 5, 34, 9 };
		vector<int> v7(arr + 1, arr + sizeof(arr) / sizeof(arr[0]));
		for (auto e : v7)
		{
			cout << e << " ";
		}
		cout << endl;

		//拷贝构造
		vector<int> v8(v7);
		for (auto e : v8)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void vector_test3()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.pop_back();
		v.pop_back();
		v.pop_back();
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.resize(10, 1);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.resize(3);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl << endl;

		vector<std::string> vs;
		vs.push_back("11111");
		vs.push_back("22222");
		vs.push_back("33333");
		vs.push_back("44444");
		vs.push_back("55555");
		for (const std::string& e : vs)
		{
			cout << e << endl;
		}
		cout << endl;

		vs.resize(10, "xxxxx");
		for (const std::string& e : vs)
		{
			cout << e << endl;
		}
		cout << endl;
	}

	void vector_test4()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		//v.push_back(5);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		//如果插入时 vector 扩容了,则迭代器 pos 会失效
		//如果还想使用 pos,则可以接收 insert 返回值
		vector<int>::iterator pos = std::find(v.begin(), v.end(), 2);
		v.insert(pos, 20);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		//如果 erase end() - 1 位置的数据,则迭代器失效
		//因此认为 erase(pos) 后,迭代器 pos 失效
		pos = v.end() - 1;
		v.erase(pos);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}
相关推荐
Ai 编码助手几秒前
使用php和Xunsearch提升音乐网站的歌曲搜索效果
开发语言·php
学习前端的小z4 分钟前
【前端】深入理解 JavaScript 逻辑运算符的优先级与短路求值机制
开发语言·前端·javascript
神仙别闹12 分钟前
基于C#和Sql Server 2008实现的(WinForm)订单生成系统
开发语言·c#
XINGTECODE13 分钟前
海盗王集成网关和商城服务端功能golang版
开发语言·后端·golang
我们的五年22 分钟前
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
linux·c++·学习
zwjapple29 分钟前
typescript里面正则的使用
开发语言·javascript·正则表达式
小五Five31 分钟前
TypeScript项目中Axios的封装
开发语言·前端·javascript
前端每日三省32 分钟前
面试题-TS(八):什么是装饰器(decorators)?如何在 TypeScript 中使用它们?
开发语言·前端·javascript
凡人的AI工具箱1 小时前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
做人不要太理性1 小时前
【C++】深入哈希表核心:从改造到封装,解锁 unordered_set 与 unordered_map 的终极奥义!
c++·哈希算法·散列表·unordered_map·unordered_set