【C++】vector的模拟实现


1、vector的模拟实现.h

c 复制代码
#pragma once

namespace My_vector
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator; //typedef受访问限定符限制,要放成公有
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish; //end和_finish都指向最后一个数的下一个
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		//无参构造
		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}

		//n个val值构造
		vector(size_t n, const T val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(int n, const T val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		//拷贝构造
		vector(const vector<T>& v)
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		//v3 = v1
		void swap(vector<int>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}

		vector<T>& operator=(vector<T> tmp) //传参用了拷贝构造
		{
			swap(tmp);
			return *this;
		}

		//迭代区间构造
		//类模板里可以套函数模板
		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		void reserve(size_t n)
		{
			size_t sz = size();
			if (n > capacity())
			{
				T* tmp = new T[n];
				if (_start)
				{
					//memcpy(tmp, _start, sizeof(T) * sz);
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}

					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		//void rsize(size_t n, T val = T())
		void rsize(size_t n, const T& val = T()) //匿名对象具有常性,要加const
		{
			if (n > size())
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
			if (n <= size())
			{
				_finish = _start + n;
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				/*size_t sz = size();
				size_t cp = capacity() == 0 ? 4 : capacity() * 2;
				T* tmp = new T[cp];

				if (_start)
				{
					memcpy(tmp, _start, sizeof(T) * sz);
					delete[] _start;
				}

				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + cp;*/

				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			++_finish;
		}

		//insert、erase操作后迭代器都会失效,不能再访问
		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len; //扩容后要更新pos的相对位置
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}
			*pos = x;
			++_finish;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				it++;
			}
			--_finish;
			return pos;
		}

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

			return _start[pos];
		}

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

			return _start[pos];
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

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

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

		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;
	}

	void Test2()
	{
		vector<int*> v1;
		v1.rsize(5);

		vector<string> v2;
		//v2.rsize(5, string("xxx")); //匿名对象
		v2.rsize(5, "xxx"); //隐式类型转换

		for (auto e : v1)
		{
			cout << e << ' ';
		}
		cout << endl;
	
		for (auto e : v2)
		{
			cout << e << ' ';
		}
		cout << endl;
	}

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

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

		v.insert(v.begin() + 2, 30);
		v.insert(v.begin(), 0);

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

		v.erase(v.begin());
		v.erase(v.begin() + 2);

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

	void Test4()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		v.push_back(6);
		v.push_back(7);
		v.push_back(8);

		auto it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{
				it = v.erase(it);
				//it = it - 1;
			}
			else
			{
				it++; //不删除再++
			}
		}

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

	void Test5()
	{
		vector<string> v;
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");
		v.push_back("111111111111111111111");

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

	void Test6()
	{
		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<int> v2(v);

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

		vector<int> v3;
		v3.push_back(3);
		v3.push_back(4);
		v3.push_back(5);
		v3.push_back(6);
		v3.push_back(7);

		v2 = v3;

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

	void Test7()
	{
		vector<int> v1(10, 15);
		vector<int> v2(v1.begin(), v1.end());
		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<string> v3(5, "xxx");
		vector<string> v4(v3.begin(), v3.end());
		for (auto e : v4)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}

2、Test.cpp

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
#include <string.h>
#include <vector>
using namespace std;

#include "1、vector的模拟实现.h"

int main()
{
	My_vector::Test7();

	return 0;
}
相关推荐
奋斗的小花生2 小时前
c++ 多态性
开发语言·c++
闲晨2 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
UestcXiye3 小时前
《TCP/IP网络编程》学习笔记 | Chapter 3:地址族与数据序列
c++·计算机网络·ip·tcp
霁月风4 小时前
设计模式——适配器模式
c++·适配器模式
jrrz08285 小时前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
咖啡里的茶i5 小时前
Vehicle友元Date多态Sedan和Truck
c++
海绵波波1075 小时前
Webserver(4.9)本地套接字的通信
c++
@小博的博客5 小时前
C++初阶学习第十弹——深入讲解vector的迭代器失效
数据结构·c++·学习
爱吃喵的鲤鱼6 小时前
linux进程的状态之环境变量
linux·运维·服务器·开发语言·c++
7年老菜鸡7 小时前
策略模式(C++)三分钟读懂
c++·qt·策略模式