首页 技术 正文
技术 2022年11月12日
0 收藏 763 点赞 4,651 浏览 2492 个字

  vector的简易实现整理自《数据结构与算法分析–C++描述(第3版)》3.4节“向量的实现”。详细可参考《STL源码分析》4.2节。

  具体实现代码如下:

 #ifndef VECTOR_H
#define VECTOR_H #include <iostream>
using namespace std; template<class T>
class Vector
{
private:
int theSize;
int theCapacity;
T *objects; public:
typedef T* iterator;
typedef const T* const_iterator;
enum{ SPARE_CAPACITY = }; public:
explicit Vector(int initSize = ); //用explicit避免隐式类型转换
~Vector();
Vector(const Vector<T>& rhs);
const Vector<T>& operator=(const Vector<T>& rhs); void resize(int newSize);
void reserve(int newCapacity); T& operator[](int index);
const T& operator[](int index) const; bool empty() const;
int size() const;
int capacity() const; void push_back(const T& x);
void pop_back();
const T& back() const; iterator begin();
const_iterator begin() const; iterator end();
const_iterator end() const; }; template<class T>
Vector<T>::Vector(int initSize = ) : theSize(initSize), theCapacity(initSize + SPARE_CAPACITY)
{
objects = new T[theCapacity];
} template<class T>
Vector<T>::~Vector()
{
delete []objects;
} template<class T>
Vector<T>::Vector(const Vector<T>& rhs) : objects(nullptr)
{
operator = (rhs);
} template<class T>
const Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
{
if (this != &rhs)
{
delete[]objects;
theSize = rhs.size();
theCapcity = rhs.theCapacity; // 注意是深拷贝
objects = new T[capacity()];
for (int k = ; k < size(); k++)
objects[k] = rhs.objects[k];
} return *this;
} template<class T>
void Vector<T>::resize(int newSize)
{
if (newSize > theCapacity)
reserve(newSize * + ); //每次空间不够的时候,就重新获得2倍于当前容量的空间,+1是为了防止0的情况
theSize = newSize;
} template<class T>
void Vector<T>::reserve(int newCapacity)
{
if (newCapacity < theSize)
return; T *oldArray = objects; // 之所以需要将老的数组复制到新的数组,是因为要保证Vector整块内存的连续性
objects = new T[newCapacity];
for (int k = ; k < theSize; k++)
objects[k] = oldArray[k]; theCapacity = newCapacity; delete []oldArray;
} template<class T>
T& Vector<T>::operator[](int index)
{
return objects[index];
} template<class T>
const T& Vector<T>::operator[](int index) const
{
return objects[index];
} template<class T>
bool Vector<T>::empty() const
{
return size() == ;
} template<class T>
int Vector<T>::size() const
{
return theSize;
} template<class T>
int Vector<T>::capacity() const
{
return theCapacity;
} template<class T>
void Vector<T>::push_back(const T& x)
{
if (theSize == theCapacity)
reserve(theCapacity * + );
objects[theSize++] = x;
} template<class T>
void Vector<T>::pop_back()
{
theSize--;
} template<class T>
const T& Vector<T>::back() const
{
return objects[theSize - ];
} template<class T>
T* Vector<T>::begin()
{
return &objects[];
} template<class T>
const T* Vector<T>::begin() const
{
return &objects[];
} template<class T>
T* Vector<T>::end()
{
return &objects[size() - ];
} template<class T>
const T* Vector<T>::end() const
{
return &objects[size() - ];
} #endif

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781