Vector的简单C++实现

本文介绍了一个使用模板元编程实现的Vector类,该类支持动态数组的基本操作,如构造、赋值、扩容、访问元素等,并通过模板泛型支持多种数据类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

template <typename Object>
class Vector
{
public:
explicit Vector(int initSize=0)
:theSize( initSize ),theCapacity( initSize + SPARE_CAPACITY)
{objects = new Object[theCapacity];}

Vector(const Vector & rhs): objects(NULL)
{ operator= (rhs);}

~Vector(void)
{delete[] objects;}

const Vector &operator= (const Vector & rhs)
{
if(this != &rhs)
{
delete[]objects;
theSize = rhs.size();
theCapacity = rhs.theCapacity;

objects = new Object[capacity()];
for(int k=0;k<size();k++)
objects[k] = rhs.objects[k];
}
return *this;
}

void resize(int newSize)
{
if(newSize > theCapacity)
{
reserve(newSize*2+1);
theSize = newSize;
}
}

void reserve(int newCapacity)
{
if(newCapacity < theSize)
return;

Object *oldArray = objects;

objects = new Object[newCapacity];
for(int k=0;k<theSize;k++)
objects[k] = oldArray[k];

theCapacity = newCapacity;

delete [] oldArray;
}

Object & operator[](int index)
{ return objects[index];}
const Object & operator[](int index) const
{ return objects[index];}

bool empty() const
{ return size() == 0;}
int size() const
{ return theSize;}
int capacity() const
{ return theCapacity;}

void push_back( const Object & x)
{
if(theSize == theCapacity)
reserve(2*theCapacity + 1);
objects[theSize++] = x;
}

void pop_back()
{ theSize--;}

const Object & back() const
{ return objects[theSize - 1];}

typedef Object * iterator;
typedef const Object * const_iterator;

iterator begin()
{ return &objects[0];}
const_iterator begin() const
{ return &objects[0];}
iterator end()
{ return &objects[size()];}
const_iterator end() const
{ return &objects[size()];}

enum{SPARE_CAPACITY = 16};

private:
int theSize;
int theCapacity;
Object * objects;
};


C++中,`std::vector` 是一个非常实用的序列容器,它能够动态地存储元素,并且能够快速地访问任何一个元素。下面是一个简化版的 `vector` 类的实现示例,仅用于演示基本概念: ```cpp template <typename T> class SimpleVector { private: T* data; // 指向动态分配数组的指针 size_t capacity; // 当前分配的存储空间大小 size_t size; // 当前存储的元素数量 public: SimpleVector() : data(nullptr), capacity(0), size(0) { // 默认构造函数 } ~SimpleVector() { delete[] data; } void push_back(const T& value) { if (size >= capacity) { // 需要扩展存储空间 reserve(capacity == 0 ? 1 : capacity * 2); } data[size++] = value; } void reserve(size_t newCapacity) { if (newCapacity > capacity) { T* newData = new T[newCapacity]; for (size_t i = 0; i < size; ++i) { newData[i] = data[i]; } delete[] data; data = newData; capacity = newCapacity; } } T& operator[](size_t index) { if (index >= size) { throw std::out_of_range("Index out of range"); } return data[index]; } const T& operator[](size_t index) const { if (index >= size) { throw std::out_of_range("Index out of range"); } return data[index]; } size_t getSize() const { return size; } }; ``` 这个简单的 `vector` 类实现了以下几个基本功能: - 动态数组的存储 - 通过 `push_back` 添加元素 - `reserve` 方法来预先分配空间,避免在添加新元素时频繁地重新分配内存 - 通过 `operator[]` 提供了快速的元素访问能力 - 析构函数中释放内存,避免内存泄漏 需要注意的是,这个简化版的 `vector` 实现并没有实现迭代器支持、异常安全性、边界检查的全面性、拷贝构造函数、赋值操作符、移动语义等完整的 `std::vector` 功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值