vector实现

Posted 在下赵某人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vector实现相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<assert.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

namespace zzd

	template<class T>
	class vector
	
	public:
		//定义迭代器
		typedef T* iterator;
		iterator begin()
		
			return _start;
		
		iterator end()
		
			return _finish;
		
		typedef const T* const_iterator;
		const_iterator begin() const
		
			return _start;
		
		const_iterator end() const
		
			return _finish;
		
		//构造函数
		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)
		
			;
		
		//带参构造函数,要写成模板,也可以传其他类的迭代器
		template<class InputIterator>
		vector(InputIterator begin, InputIterator end)
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)//要用push_back,所以要将成员变量初始化为nullptr
		
			while (begin != end)//遍历迭代器区间,将数据一个个插入
			
				push_back(*begin);
				++begin;
			
		
	
		拷贝构造(传统写法)
		//vector(const vector<T>& v)
		//	:_start(nullptr)
		//	,_finish(nullptr)
		//	,_end_of_storage(nullptr)//要把成员变量先初始化为空
		//
		//	reserve(v.capacity());//提前开辟好空间
		//	for (auto& e : v)
		//	
		//		push_back(e);//push_back里会完成深拷贝
		//	
		//
		
		//拷贝构造(现代写法)
		vector(const vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		
			//调用构造函数构造一个临时变量
			vector<T> tmp(v.begin(), v.end());
			//交换临时变量与该变量的资源
			swap(tmp);
		
		void swap(vector<T>& v)
		
			::swap(_start, v._start);
			::swap(_finish, v._finish);
			::swap(_end_of_storage, v._end_of_storage);
		

		//赋值重载
		vector<int>& operator=(vector<T> v)
		
			swap(v);
			return *this;
		

		size_t size() const//函数内部若不会改变对象,最好用const修饰该函数,
		                  //这样const对象和非const对象都可以调用该函数
			return _finish - _start;
		
		size_t capacity() const
		
			return _end_of_storage - _start;
		
		//[]操作符重载
		T& operator[](size_t i)
		
			assert(i < size());
			return _start[i];
		
		const T& operator[](size_t i) const
		
			assert(i < size());
			return _start[i];
		

		void reserve(size_t n)
		
			if (n > capacity())
			
				T* tmp = new T[n];
				size_t sz = size();
				memcpy(tmp, _start, sizeof(T) * sz);
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			
		
		void resize(size_t n, const T& val = T())//T()是匿名对象,若为内置类型会全设为零;若为自定义类型会调用构造函数构造一个临时对象。
		
			if (n <= size())
			
				_finish = _start + n;
			
			else
			
				if (n > capacity())
				
					reserve(n);
				
				while (_finish < _start + n)
				
					*_finish = val;
					++_finish;
				
			
		
		
		bool empty()
		
			return _start == _finish;
		
		void push_back(const T& x)
		
			//判断是否需要扩容
			if (_finish == _end_of_storage)
			
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			
			//插入数据
			*_finish = x;//这里x若为自定义类型,则会调用它自己的赋值重载实现深拷贝
			_finish++;
				
		void pop_back()
		
			assert(!empty());
			_finish--;
		

		iterator insert(iterator pos, const T& x)
		
			assert(pos > _start && pos < _finish);
			//判断是否需要请扩容
			if (_finish == _end_of_storage)
			
				size_t len = pos - _start;//记录下pos和_start的相对位置
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;//对pos完成迭代,防止迭代器失效
			
			//从后往前挪动数据
			iterator end = _finish - 1;
			while (end >= pos)
			
				*(end + 1) = *end;
				end--;
			
			//pos位置插入数据
			*pos = x;
			_finish++;
			return pos;
		
		iterator erase(iterator pos)
		
			assert(pos >= _start && pos < _finish);
			//从前往后向前挪动
			iterator begin = pos;
			while (begin != _finish)
			
				*begin = *(begin + 1);
				begin++;
			
			_finish--;
			return pos;
		
	private:
		T* _start;
		T* _finish;
		T* _end_of_storage;
	;

	/*template<class T>*/
	void print(const vector<int>& v)
	
		cout << "迭代器:";
		vector<int>::const_iterator it = v.begin();
		while (it != v.end())
		
			cout << *it << " ";
			++it;
		
		cout << endl;
		cout << "范围for:";
		for (auto& e : v)
		
			cout << e << " ";
		
		cout << endl;

		cout << "[]访问遍历:";
		for (size_t i = 0; i < v.size(); i++)
		
			cout << v[i] << " ";
		
		cout << endl;
		cout << endl;
	

	void test_vector1()
	
		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);
		print(v);
		vector<int>::iterator pos = find(v.begin(), v.end(), 3);
		for (int i = 0; i < 3; i++)
		
			pos = v.insert(pos, 33);
		
		print(v);

		//v.pop_back();
		//v.pop_back();
		//print(v);

		vector<int>::iterator it = v.begin();
		while (it != v.end())
		
			if (*it == 33)
			
				v.erase(it);
			
			else
			
				it++;
			
		
		print(v);
	


int main()

	zzd::test_vector1();
	return 0;

今天的xdm不一定学懂了,但是一定学的很爽,但是,爽就够了!

 给个赞,下次还给兄弟们分享学习资料

以上是关于vector实现的主要内容,如果未能解决你的问题,请参考以下文章

13. vector容器迭代器iterator自实现

C++初阶vector(下)

C++初阶vector(下)

C++初阶vector(下)

[读书笔记·编程珠玑] 数组移位(待填坑)

利用STL实现动态数组