【C++】vector的模拟实现

【C++】vector的模拟实现_第1张图片


1、vector的模拟实现.h

#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

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 
#include 
using namespace std;

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

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

	return 0;
}

你可能感兴趣的:(C++,c++)