栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

vector的底层模拟实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vector的底层模拟实现

文章目录
    • vector的成员变量
    • 迭代器的模拟实现
    • 获取size,capacity,判空操作
    • reserve和resize的模拟实现(重点)
    • pop_back (尾删)和 push_back(尾插)
    • insert(任意位置的插入) 和 erase (任意位置的删除)
    • 构造,析构,拷贝构造,赋值重载
    • 关于当扩容的时候用memcpy会引发的一些问题

vector的成员变量

什么是vector呢? vector是可变数组大小的序列容器。(跟模拟实现顺序表差不多)
模拟实现顺序表的成员变量:指针,size,capacity。

迭代器的模拟实现
iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			reutrn _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			reutrn _finish;
		}
获取size,capacity,判空操作

不修改数据的时候,我们建议都加上const

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

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

		bool empty() const
		{
			return _start == _finish;
		}
reserve和resize的模拟实现(重点)

reserve针对capacity,当要调整的空间大于capacity时才会调整,小于capacity不做处理(但是不一定会调整到指定大小,
因为无论是C++编译器还是Linux编译器都有自己的增容方式)
resize针对于size ,如果要调整的个数小于size,直接砍掉。 但是调整的个数大于size,扩容后填数据

void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				//memcpy(tmp,_start,sizeof(T)*size()) 如果用memcpy会出现一些错误
				//这个放到最后面再说
				if (_start)
				{
					for (size_t i = 0; i < sz; i++)
					{
						tmp[i] = _start[i];
					}

					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;    //这里的_finis , _endofstorage都要重新刷新一下
				_endofstorage = _start + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}

			else
			{
				if (n > capacity())
				{
					reserve(n);
			    }

				while (size() < n)
				{
					*_finish++ = val;
				}
			}
		}
pop_back (尾删)和 push_back(尾插)
void push_back(const T& val)
		{
			if (_finish == _endofstorage)  //这里是判断是否满了,不要用_start去比较了
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			
			*_finish = val;
			_finish++;
		}

		void pop_back()
		{
			if (empty())
			{
				cout << "no data to delete" << endl;
				return;
			}
			else
			{
				_finish--;
			}
		}
insert(任意位置的插入) 和 erase (任意位置的删除)
         iterator insert(iterator pos, const T& val)
		{
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);

				//这里如果发生了扩容,pos就要刷新一下
				//否则就失效了
				pos = _start + len;
			}

			iterator it = _finish - 1;
			while (it >= pos)
			{
				*(it + 1) = *it;
				--it;
			}
			*pos = val;
			_finish++;

			return pos;
		}

		void insert(iterator pos,size_t n ,const T& val)
		{
			size_t max = n + size();
			if ( max > capacity())
			{
				size_t len = pos - _start;

				reserve(max);

				//这里如果发生了扩容,pos就要刷新一下
				//否则就失效了
				pos = _start + len;
			}

			size_t len = n;
			iterator it = _finish - 1;
			while (it >= pos)
			{
				*(it + len) = *it;
				it--;
			}

			while (n)
			{
				n--;
				*pos = val;
				pos++;
			}

			_finish = _finish + len;
		}

		iterator erase(iterator pos)
		{
			if (empty())
			{
				cout << " no data to delete" << endl;
			}
            
			iterator it = pos ;
			while (it < _finish-1)
			{
				*it = *(it + 1);
				it++;
			}
			_finish--;
			return pos;
		}
构造,析构,拷贝构造,赋值重载
vector()
			:_start(nullptr)
		    , _finish(nullptr)
			,_endofstorage(nullptr)
			{}

		vector(size_t n, const T& val = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			_start = new T[n];
			for (size_t i = 0; i < n; i++)
			{
				_start[i] = val;
			}
			_finish = _start + n;
			_endofstorage = _start + n;
		}

		// v1(v2)
		vector(const vector& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			_start = new T[v.capacity()];
			for (size_t i = 0; i < v.size(); i++)
			{
				_start[i] = v._start[i];
			}
			_finish = _start + v.size();
			_endofstorage = _start + v.capacity();
		}

		//v1 = v2
		vector& operator=(vector v)
		{
			swap(v);
			return *this;
		}
		
        //类模板里面的模板函数
		template
		vector(InputIterator first, InputIterator last)
		{
			int len = last - first;
			_start =_finish = new T[len];
			while (first != last)
			{
				*_finish++ = *first++;
			}
			_endofstorage = _start + len;
		}

		void swap(vector& v)
		{
			::swap(_start, v._start);
			::swap(_finish, v._finish);
			::swap(_endofstorage, v._endofstorage);
		}
关于当扩容的时候用memcpy会引发的一些问题



总结:如果对象中涉及到资源管理时,千万不能使用memcpy进行对象之间的拷贝,因为memcpy是
浅拷贝,否则可能会引起内存泄漏甚至程序崩溃。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1001523.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号