Vector底层功能的实现

Vector底层功能的实现

Vector底层功能的实现

7/23/2020,上班已经20多天了,该写点东西来记录自己写的东西了。

Vector的功能

Vector是C++中基础的功能,但是其底层的实现,在网上只有应用,而没有怎么做。
这次实现的功能。

  1. begin()
  2. end();
  3. clear();
  4. 全新的 KaTeX数学公式 语法;
  5. pushback();
  6. pop_back();
  7. insert();
  8. assign()。
  9. erase();
  10. .empty();
  11. size();
    代码的实现如下
#include<iostream>
#include<assert.h>
#include<algorithm>
#ifndef _CVECTOR_H_
#define _CVECTOR_H_

namespace username
{
   
	class NoCopy
	{
   
	public:
		inline NoCopy() {
   }
		NoCopy(const NoCopy&);
		NoCopy& operator=(const NoCopy&);
	};

	template<typename T>
	class viterator :public std::iterator<std::forward_iterator_tag, T>
	{
   
	public:
		viterator()
		{
   
			t = NULL;
		}
		viterator(T* t_)
		{
   
			t = t_;
		}
		viterator(const viterator& other)
		{
   
			t = other.t;
		}
		viterator& operator=(const viterator& other)
		{
   
			t = other.t;
			return *this;
		}

		viterator& operator++()
		{
   
			t++;
			return *this;
		}
		viterator operator++(int)
		{
   
			viterator iter = *this;
			t++;
			return iter;
		}
		viterator operator+(int count)
		{
   
			viterator iter = *this;
			iter.t += count;
			return iter;
		}
		viterator& operator--()
		{
   
			t--;
			return *this;
		}
		viterator operator--(int)
		{
   
			viterator iter = *this;
			t--;
			return iter;
		}
		viterator operator-(int count)
		{
   
			viterator iter = *this;
			iter.t -= count;
			return iter;
		}

		int operator-(const viterator& other)
		{
   
			return t - other.t;
		}
		int operator-(const viterator& other)const
		{
   
			return t - other.t;
		}

		T& operator*()
		{
   
			return *t;
		}
		const T& operator*() const
		{
   
			return *t;
		}

		T* operator->()
		{
   
			return t;
		}
		const T* operator->() const
		{
   
			return t;
		}

		inline bool operator!=(const viterator& other)
		{
   
			return t != other.t;
		}
		inline bool operator!=(const viterator& other)const
		{
   
			return t != other.t;
		}

		inline bool operator==(const viterator& other)
		{
   
			return t == other.t;
		}
		inline bool operator==(const viterator& other)const
		{
   
			return t == other.t;
		}

		inline bool operator<(const viterator& other)
		{
   
			return t < other.t;
		}
		inline bool operator<(const viterator& other)const
		{
   
			return t < other.t;
		}

		inline bool operator<=(const viterator& other)
		{
   
			return t <= other.t;
		}
		inline bool operator<=(const viterator& other)const
		{
   
			return t <= other.t;
		}

		inline bool operator>(const viterator& other)
		{
   
			return t > other.t;
		}
		inline bool operator>(const viterator& other)const
		{
   
			return t > other.t;
		}

		inline bool operator>=(const viterator& other)
		{
   
			return t >= other.t;
		}
		inline bool operator>=(const viterator& other)const
		{
   
			return t >= other.t;
		}
	private:
		T* t;
	};


	template<typename T>
	class cvector :public NoCopy
	{
   
	public:
		typedef viterator<T> iterator;//viterator<T>就是对一个指针的包装,所以完全可以用T*代替viterator <T>
		typedef const viterator<T> const_iterator;

		//typedef T* iterator;
		//typedef const T* const_iterator;
		cvector()
		{
   
			initData(0);
		}
		cvector(int capa, const T& val = T())
		{
   
			initData(capa);
			newCapacity(capacity_);
			for (int i = 0; i < size_; i++)
				buf[i] = val;
		}
		cvector(const_iterator first, const_iterator last)
		{
   
			initData(last - first);
			newCapacity(capacity_);
			iterator iter = iterator(first);
			int index = 0;
			while (iter != last)
				buf[index++] = *iter++;
		}

		~cvector()
		{
   
			if (buf)
			{
   
				delete[] buf;
				buf = NULL;
			}
			size_ = capacity_ = 0;
		}
		void clear()
		{
   
			if (buf)
				erase(begin(), end());
		}

		void push_back(const T& t)
		{
   
			if (size_ == capacity_)
			{
   
				int capa = calculateCapacity();
				newCapacity(capa);
			}
			buf[size_++] = t;
		}
		void pop_back()
		{
   
			if (!empty())
				erase(end() - 1);
		}
		//insert;整段的插入//for循环赋值或memmove移,有三个参数,(A插入位置i1,要B的初始位置i2,B结束位置i3)
		void insert2()
		{
   
			viterator//建立C,C的大小变为A+(B.end-b.begin)
			//for(i=0;i=A的插入位置;i++)
			{
   
				//赋值为A的值
			}
			//for(i=i1;i=i1+i3-i2;i++)
			{
   

			}
			//for(i=i1+i3-i2;i=size;i++)
			{
   

			}
			//return C
		}
		/*vect.insert(iter, Point(77, 77));*/
		int insert(const_iterator iter
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值