欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++ vector(模拟实现)

程序员文章站 2022-03-23 14:12:57
...
#include<iostream>
using namespace std;

namespace bit
{
	template<typename T>
	class vector
	{
	public:
		typedef T* iterator;
	public:
		T operator[](int i)
		{
			return start[i];
		}
	public:
		vector() :start(nullptr), finish(nullptr), end_of_sorage(nullptr)
		{
		}
		vector(size_t n, const T& value = T()) :start(nullptr), finish(nullptr), end_of_sorage(nullptr)
		{
			reserve(n);//先扩容
			while (n--!=0) //再填充
			{
				push_back(value);
			}
		}
		template<class InPutIterator>  //由前后指针来创建
		vector(InPutIterator first, InPutIterator last):start(nullptr), finish(nullptr), end_of_sorage(nullptr)
		{
			reserve(last-first);//先申请空间
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}
		~vector()
		{
			delete[]start;
			start = finish = end_of_sorage = nullptr;
		}
	public:
		int size()
		{
			return finish - start;
		}
		int capacity()
		{
			return end_of_sorage - start;
		}
		bool empty()
		{
			return finish == start;
		}
		void swap(vector<T>& v)
		{
			std::swap(start, v.start);
			std::swap(finish, v.finish);
			std::swap(end_of_sorage, v.end_of_sorage);
		}
		void reserve(size_t new_capacity) // 扩容
		{
			if (new_capacity > capacity())
			{
				int old_size = size();  //原来的大小 
				T* newV = new T[new_capacity];  //新申请空间
				if (start)//当原有内容不空时
				{
					for (int i = 0; i < size(); i++)  //复制进新空间
					{
						newV[i] = start[i];
					}
				}
				delete[]start;//删除原有空间
				start = newV;//指向新空间
				finish = start + old_size;
				end_of_sorage = start + new_capacity;
			}
		}
		void resize(int new_size, const T& value = 0) //扩充大小
		{
			if (new_size <= size())
			{
				finish = start + new_size;
			}
			if (new_size > capacity())
			{
				reserve(new_size * 2);
			}
			iterator p = finish;
			finish = start + new_size;//指向新大小
			while (p != finish) //填充value
			{
				*p = value;
				p++;
			}
		}
	public:
		void push_back(const T &c)
		{
			insert(end(), c);
		}
	public:
		typedef T* iterator;
		iterator begin()
		{
			return start;
		}
		iterator end()
		{
			return finish;
		}
	public:
		iterator insert(iterator pos, const T &x) //在pos位置前插入x
		{
			if (size() + 1 >= capacity())
			{
				size_t oldpos = pos - start;
				size_t new_capacity = capacity() ? (capacity() * 2) : 1;
				reserve(new_capacity);
				pos = start + oldpos;
			}
			T* p = finish;
			for (; p != pos; p--)
			{
				*p = *(p - 1);
			}
			*p = x;
			finish++;
			return pos;
		}
		iterator erase(iterator pos) //删除pos位置值
		{
			T* p = pos;
			while (p != finish - 1)
			{
				*p = *(p + 1);
				p++;
			}
			finish--;
			return pos;
		}
	private:
		T* start;//指向最开始
		T* finish;//指向最后一个元素的下一个位置
		T* end_of_sorage;//指向最大容量的下一个位置
	};
}

int main()
{
	int ar[] = { 1,2,3,4,5,6,7,7 };
	bit::vector<int>v1(ar, ar + 6);
	bit::vector<int>v2;
	bit::vector<int>v3(10,'a');
	v1.erase(v1.end()-1);
	v1.insert(v1.begin(), 0);
	v1.swap(v3);
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	return 0;
}
相关标签: 学习笔记 c++