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, const T& t)
{
int index = iter - begin();
if (index <= size_)
{
if (size_ == capacity_)
{
int capa = calculateCapacity();
newCapacity(capa);
}
memmove(buf + index + 1, buf + index, (size_ - index) * sizeof(T));
buf[index] = t;
size_++;
}
return index;
}//assign,一个容器整个赋值到另一个,
int assign(const_iterator iter, const_iterator iteme)
{
iter.clear();//清空第一个
insert2(A.begin,b.begin,b.end)
return iter//返回第一个
}
///123
///321
///3214
iterator erase(const_iterator iter)
{
int index = iter - begin();
if (index < size_ && size_>0)
{
memmove(buf + index, buf + index + 1, (size_ - index) * sizeof(T));
buf[--size_] = T();
}
return iterator(iter);
}
iterator erase(const_iterator first, const_iterator last)
{
iterator first_ = iterator(first);
iterator last_ = iterator(last);
while (first_ <= last_--)
erase(first_);
return iterator(first_);
}
T& front()
{
assert(size_ > 0);
return buf[0];
}
T& back()
{
assert(size_ > 0);
return buf[size_ - 1];
}
T& at(int index)
{
assert(size_ > 0);
return buf[index];
}
T& operator[](int index)
{
assert(size_ > 0 && index >= 0 && index < size_);
return buf[index];
}
bool empty() const
{
return size_ == 0;
}
int size() const
{
return size_;
}
int capacity() const
{
return capacity_;
}
iterator begin()
{
return iterator(&buf[0]);
}
iterator end()
{
return iterator(&buf[size_]);
}
private:
void newCapacity(int capa)
{
capacity_ = capa;
T* newBuf = new T[capacity_];
if (buf)
{
memcpy(newBuf, buf, size_ * sizeof(T));
delete[] buf;
}
buf = newBuf;
}
inline int calculateCapacity()
{
return capacity_ * 3 / 2 + 1;
}
inline void initData(int capa)
{
buf = NULL;
size_ = capacity_ = capa > 0 ? capa : 0;
}
int size_;
int capacity_;
T* buf;
};
struct Point
{
Point(int x_ = 0, int y_ = 0) :x(x_), y(y_) {}
int x, y;
};
bool operator<(const Point& p1, const Point& p2)
{
if (p1.x < p2.x)
{
return true;
}
else if (p1.x > p2.x)
{
return false;
}
return p1.y < p2.y;
}
void cvectorTest()
{
cvector<Point> vect;
for (int i = 0; i < 10; i++)
{
Point p(i, i);
vect.push_back(p);
}
cvector<Point>::iterator iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
iter = vect.begin() + 5;
vect.insert(iter, Point(55, 55));
iter = vect.end() - 3;
vect.insert(iter, Point(77, 77));
std::cout << std::endl << "插入两个元素后:" << std::endl;
iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
std::sort(vect.begin(), vect.end());
std::cout << std::endl << std::endl << "排序后:" << std::endl;
iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
vect.erase(vect.begin() + 10);
vect.erase(vect.begin() + 10);
std::cout << std::endl << std::endl << "删除之前新增的两个元素" << std::endl;
iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
vect.clear();
std::cout << std::endl << std::endl << "执行clear之后" << std::endl;
std::cout << "size=" << vect.size() << ",capacity=" << vect.capacity();
cvector<Point> vect1;
for (int i = 10; i < 20; i++)
{
Point p(i, i);
vect1.push_back(p);
}
std::cout << std::endl << std::endl << "从别的cvector复制数据:" << std::endl;
cvector<Point> vect2(vect1.begin(), vect1.end());
vect2.pop_back();
vect2.pop_back();
for (int i = 0; i < vect2.size(); i++)
{
std::cout << "[" << vect2[i].x << "," << vect2[i].y << "], ";
}
std::cout << std::endl;
}
}
int main()
{
username::cvectorTest();
return 0;
}
//修改完成后
#include<iostream>
#include<assert.h>
#include<algorithm>
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(iterator pos , iterator begin1, iterator end1)
{
//建立C,C的大小变为A+(B.end-b.begin)
int pos1 = pos - begin();
int index = size_ + (begin1 - end1);
while (index >= capacity_)
{
int capa = calculateCapacity();
newCapacity(capa);
}
iterator pos2 = begin() + pos1;
memmove(&(*(pos2+(end1-begin1))),&( *(pos2)), (end()-pos2) * sizeof(T));
memmove(&(*(pos2)), &(*(begin1)), (end1 - begin1) * sizeof(T));
size_+=(end1-begin1);
}
/*vect.insert(iter, Point(77, 77));*/
int insert(const_iterator iter, const T& t)
{
int index = iter - begin();
if (index <= size_)
{
if (size_ == capacity_)
{
int capa = calculateCapacity();
newCapacity(capa);
}
memmove(buf + index + 1, buf + index, (size_ - index) * sizeof(T));
buf[index] = t;
size_++;
}
return index;
}//assign,一个容器整个赋值到另一个,
int assign(const_iterator iter, const_iterator iteme)
{
iter.clear();//清空第一个
iter=insert2(A.begin,b.begin,b.end)
return iter//返回第一个
}
///123
///321
///3214
iterator erase(const_iterator iter)
{
int index = iter - begin();
if (index < size_ && size_>0)
{
memmove(buf + index, buf + index + 1, (size_ - index) * sizeof(T));
buf[--size_] = T();
}
return iterator(iter);
}
iterator erase(const_iterator first, const_iterator last)
{
iterator first_ = iterator(first);
iterator last_ = iterator(last);
while (first_ <= last_--)
erase(first_);
return iterator(first_);
}
T& front()
{
assert(size_ > 0);
return buf[0];
}
T& back()
{
assert(size_ > 0);
return buf[size_ - 1];
}
T& at(int index)
{
assert(size_ > 0);
return buf[index];
}
T& operator[](int index)
{
assert(size_ > 0 && index >= 0 && index < size_);
return buf[index];
}
bool empty() const
{
return size_ == 0;
}
int size() const
{
return size_;
}
int capacity() const
{
return capacity_;
}
iterator begin()
{
return iterator(&buf[0]);
}
iterator end()
{
return iterator(&buf[size_]);
}
private:
void newCapacity(int capa)
{
capacity_ = capa;
T* newBuf = new T[capacity_];
if (buf)
{
memcpy(newBuf, buf, size_ * sizeof(T));
delete[] buf;
}
buf = newBuf;
}
inline int calculateCapacity()
{
return capacity_ * 3 / 2 + 1;
}
inline void initData(int capa)
{
buf = NULL;
size_ = capacity_ = capa > 0 ? capa : 0;
}
int size_;
int capacity_;
T* buf;
};
struct Point
{
Point(int x_ = 0, int y_ = 0) :x(x_), y(y_) {}
int x, y;
};
bool operator<(const Point& p1, const Point& p2)
{
if (p1.x < p2.x)
{
return true;
}
else if (p1.x > p2.x)
{
return false;
}
return p1.y < p2.y;
}
void cvectorTest()
{
cvector<Point> vect;
for (int i = 0; i < 10; i++)
{
Point p(i, i);
vect.push_back(p);
}
cvector<Point>::iterator iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
iter = vect.begin() + 5;
vect.insert(iter, Point(55, 55));
iter = vect.end() - 3;
vect.insert(iter, Point(77, 77));
std::cout << std::endl << "插入两个元素后:" << std::endl;
iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
std::sort(vect.begin(), vect.end());
std::cout << std::endl << std::endl << "排序后:" << std::endl;
iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
vect.erase(vect.begin() + 10);
vect.erase(vect.begin() + 10);
std::cout << std::endl << std::endl << "删除之前新增的两个元素" << std::endl;
iter = vect.begin();
while (iter != vect.end())
{
std::cout << "[" << iter->x << " " << iter->y << "], ";
++iter;
}
vect.clear();
std::cout << std::endl << std::endl << "执行clear之后" << std::endl;
std::cout << "size=" << vect.size() << ",capacity=" << vect.capacity();
cvector<Point> vect1;
for (int i = 10; i < 20; i++)
{
Point p(i, i);
vect1.push_back(p);
}
std::cout << std::endl << std::endl << "从别的cvector复制数据:" << std::endl;
vect.insert2(vect.begin(),vect1.begin(),vect1.end() );
cvector<Point> vect2(vect1.begin(), vect1.end());
vect2.pop_back();
vect2.pop_back();
for (int i = 0; i < vect2.size(); i++)
{
std::cout << "[" << vect2[i].x << "," << vect2[i].y << "], ";
}
std::cout << std::endl;
}
}
int main()
{
username::cvector<int> vect;
username::cvector<int> train;
vect.push_back(1);
vect.push_back(6);
vect.push_back(8);
train.push_back(897);
train.push_back(5751);
train.push_back(12);
train.push_back(98);
vect.insert2(vect.begin(), train.begin(), train.end());
for (int i = 0; i < vect.size(); i++)
std::cout << vect[i] << std::endl;
return 0;
}
在实现的过程中insert2出现报错,原因是iterator为自己定义的类,而不是直接用的从而让memmove函数不能识别,通过对其进行&(*(变量))的方法,即解引用(指针(变量))的方式,让memmove的功能进行了实现。
link](https://www.cnblogs.com/hlxs/p/3737687.html)。本文的实现基于这篇文章的基础上,进行了改动。有需求的可以去看看。