Vector底层功能的实现
Vector底层功能的实现
7/23/2020,上班已经20多天了,该写点东西来记录自己写的东西了。
Vector的功能
Vector是C++中基础的功能,但是其底层的实现,在网上只有应用,而没有怎么做。
这次实现的功能。
- begin()
- end();
- clear();
- 全新的 KaTeX数学公式 语法;
- pushback();
- pop_back();
- insert();
- assign()。
- erase();
- .empty();
- 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