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;
	}
}
相关推荐
SRY122404192 小时前
javaSE面试题
java·开发语言·面试
李元豪3 小时前
【智鹿空间】c++实现了一个简单的链表数据结构 MyList,其中包含基本的 Get 和 Modify 操作,
数据结构·c++·链表
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
放逐者-保持本心,方可放逐3 小时前
react 组件应用
开发语言·前端·javascript·react.js·前端框架
UestcXiye3 小时前
《TCP/IP网络编程》学习笔记 | Chapter 9:套接字的多种可选项
c++·计算机网络·ip·tcp
一丝晨光4 小时前
编译器、IDE对C/C++新标准的支持
c语言·开发语言·c++·ide·msvc·visual studio·gcc
阮少年、4 小时前
java后台生成模拟聊天截图并返回给前端
java·开发语言·前端
代码小鑫4 小时前
A027-基于Spring Boot的农事管理系统
java·开发语言·数据库·spring boot·后端·毕业设计
丶Darling.5 小时前
Day40 | 动态规划 :完全背包应用 组合总和IV(类比爬楼梯)
c++·算法·动态规划·记忆化搜索·回溯