这篇文章主要为大家详细介绍了C++动态数组模版类Vector实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
1.实现机制
内部主要通过m_capacity数组容量成员和m_length数组有效长度成员来维护一个T* data数组空间.
内部默认分配一定数量大小的数组指针,每次append尾部追加的时候,无需再次分配空间,直接赋值标志length长度,假如超过当前空间容量,则再次扩大分配新的内存数组,并将旧数组拷贝至新数组及释放旧数组.
Vector需要实现的public函数如下所示:
inline int capacity()
: 获取容量inline int length() :
获取有效长度void resize(int asize) :
改变数组的有效长度void append(const T &t) :
尾部追加一个元素T& operator[] (int i) :
通过[]获取元素T operator[] (int i) const :
通过[]获取常量元素void clear() :
清空数组中的数据inline bool isEmpty():
数组是否有数据
resize()函数实现细节:
- 如果resize长度大于当前容量时 : 则扩大分配新的内存数组,并将旧数组拷贝至新数组及释放旧数组.
- 如果resize长度小于当前length时 : 则需要将多余的成员进行释放,调用析构函数实现.
- 如果resize长度大于当前length时 : 则需要调用默认构造函数来填充内部数组.
2.代码实现
#ifndef VECTOR_H
#define VECTOR_H
#include "throw.h"
// throw.h里面定义了一个ThrowException抛异常的宏,如下所示:
//#include <iostream>
//using namespace std;
//#define ThrowException(errMsg) {cout<<__FILE__<<" LINE"<<__LINE__<<": "<<errMsg<<endl; (throw errMsg);}
template <typename T>
class Vector
{
T* m_data;
int m_length; // 有效数据的长度
int m_capacity; // 分配容量的长度
// 分配
T* allocate(int size)
{
T* arr = new T[size];
if(arr == NULL) {
ThrowException( "No memory to create DynamicArray object ...");
}
return arr;
}
// 重新分配
void realloc(int capacity)
{
T* newData = allocate(capacity);
for(int i=0; i<m_length; i++) {
newData[i] = m_data[i];
}
delete[] m_data;
m_data = newData;
m_capacity = capacity;
}
// 调用析构函数
void destruct(int from, int end)
{
while(from++<end) {
m_data[from].~T();
}
}
// 调用默认构造函数
void defaultConstruct(int from, int end)
{
while(from++<end) {
m_data[from] = T();
}
}
public:
Vector(int lenght = 50) { m_length = 0; m_data = allocate(lenght); m_capacity = lenght; }
inline int capacity() const { return m_capacity; } // 获取容量
inline int size() { return m_length; } // 获取有效长度
inline int length() { return size(); }
inline T *data() { return m_data; }
inline const T *data() const { return m_data; }
inline bool isEmpty() const { return m_length == 0; }
void clear()
{
if(!m_length) return;
destruct(0, m_length);
m_length = 0;
}
void resize(int asize)
{
if(asize == m_length) return;
// 重新分配的大小>当前容量时
if(asize > m_capacity) {
realloc(asize);
}
if (asize < m_length) // 分配的大小<当前大小时,则调用析构
destruct(asize, m_length);
else // 分配的大小>当前大小时,则调用默认构造
defaultConstruct(m_length, asize);
m_length = asize;
}
// 尾部追加一个元素
void append(const T &t)
{
if(m_length == m_capacity) {
realloc(m_capacity+20); // 如果容量满了,则默认增加20个容量.方便后面append无需再次分配内存
}
m_data[m_length] = t;
m_length++;
}
T& operator[] (int i)
{
if((0 <= i) && (i < length()))
{
return m_data[i];
}
else
{
ThrowException("Parameter i is invalid ...");
}
}
T operator[] (int i) const
{
return m_data[i];
}
};
#endif // VECTOR_H
3.测试运行
测试如下所示:
class Test {
public:
int number;
Test(int n = 0) {
number = n;
}
};
int main(int argc, char *argv[])
{
Vector<Test> arr;
for(int i = 0; i < 10; i++)
arr.append(Test(i));
cout<<"********* Arr Len:"<<arr.length()<<" capacity:"<<arr.capacity()<<endl;
for(int i = 0; i < arr.length(); i++)
cout<<"arr []:"<<arr[i].number<<endl;
cout<<"*********"<<endl;
arr.resize(13);
cout<<"********* Arr Len:"<<arr.length()<<" capacity:"<<arr.capacity()<<endl;
for(int i = 0; i < arr.length(); i++)
cout<<"arr []:"<<arr[i].number<<endl;
cout<<"*********"<<endl;
arr.resize(5);
cout<<"********* Arr Len:"<<arr.length()<<" capacity:"<<arr.capacity()<<endl;
for(int i = 0; i < arr.length(); i++)
cout<<"arr []:"<<arr[i].number<<endl;
cout<<"*********"<<endl;
return 0;
}
运行如下所示:
可以看到我们resize(13)后,由于 resize长度大于当前arr的length,所以则调用默认构造函数来填充内部数组.所以arr[10]至arr[12]的number为0。
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程学习网的更多内容!
沃梦达教程
本文标题为:C++ 动态数组模版类Vector实例详解


猜你喜欢
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C语言qsort()函数的使用方法详解 2023-04-26
- Easyx实现扫雷游戏 2023-02-06
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- C++ 数据结构超详细讲解顺序表 2023-03-25
- ubuntu下C/C++获取剩余内存 2023-09-18
- Qt计时器使用方法详解 2023-05-30
- C语言详解float类型在内存中的存储方式 2023-03-27