动态数组(C++)
时间:2022-05-26
本文章向大家介绍动态数组(C++),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include<iostream>
#include <sstream>
#include<iterator>
#include<algorithm>
using namespace std;
// 重写copy代码
template<class iterator>
void copy(iterator start, iterator end, iterator to) {
// 从start复制到[to, to+end-start)
while (start != end)
*(to++) = *(start++);
}
template<class T>
class linearList {
public:
virtual ~linearList() = default;;
virtual bool empty() const = 0; /* 返回true,当且仅当线性表为空 */
virtual int size() const = 0; /* 返回线性表的元素个数 */
virtual T &get(int theIndex) const = 0; /* 返回索引为theIndex的元素 */
virtual int indexOf(const T &theElement) const = 0; /* 返回元素theElement第一次出现时的索引 */
virtual void erase(int theIndex) = 0; /* 删除索引为theIndex的元素 */
virtual void insert(int theIndex, const T &theElement) = 0;/* 把theElement插入线性表中索引为theIndex的位置上 */
virtual void output(ostream &out) const = 0;/*把线性表插入输出流*/
};
class illegalParameterValue {
public:
illegalParameterValue() : message("Illegal parameter value") {}
explicit illegalParameterValue(char *theMessage) {
message = theMessage;
}
explicit illegalParameterValue(const string &theMessage) {
message = theMessage;
cout << message << endl;
}
void outputMessage() { cout << message << endl; }
private:
string message;
};
/* 改变一个一位数组的长度*/
template<class T>
void changeLength1D(T *&a, int oldLength, int newLength) {
if (newLength < 0) /* 如果新表长小于0 */
throw illegalParameterValue("new length muse be >= 0"); /* 抛出异常*/
T *temp = new T[newLength]; /* 申请新的数组空间*/
int number = min(oldLength, newLength); /*过的原来数组长度和现在数组长度中较小者*/
copy(a, a + number, temp); /* 将原数组中的数据放到新的数组中*/
delete[] a; /* 删除原来数组的空间*/
a = temp; /*将新数组空间的地址给原来的数组名*/
}
/* 类arrayList定义
* 定义抽象类linearList的派生类arrayList
* 来实现抽象数据类型linearList
*/
template<class T>
class arrayList : public linearList<T> {
public:
/* 构造函数,复制构造函数和构析函数 */
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T> &);
~arrayList() { delete[]element; }
/* ADT方法 */
bool empty() const { return listSize == 0; }
int size() const { return listSize; }
T &get(int theIndex) const;
int indexOf(const T &theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T &theElement);
void output(ostream &out) const;
int capacity() const { return arrayLength; }
protected:
void checkIndex(int theIndex) const;
T *element; /* 存储线性表元素的一维数组 */
int arrayLength; /* 一维数组的容量 */
int listSize; /* 线性表的元素个数 */
};
template<class T>
void arrayList<T>::checkIndex(int theIndex) const {
if (theIndex < 0 || theIndex >= listSize) {
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw illegalParameterValue(s.str());
}
}
template<class T>
T &arrayList<T>::get(int theIndex) const {
// 返回索引为theIndex的元素
// 若此元素不存在,则抛出异常
checkIndex(theIndex);
return element[theIndex];
}
template<class T>
int arrayList<T>::indexOf(const T &theElement) const {
// 返回元素theElement第一次出现时的索引
// 若该元素不存在,则返回-1
// 查找元素theElement
int theIndex = (int) (find(element, element + listSize, theElement) - element);
// 确定theElement是否找到
if (theIndex == listSize)
// 没有找到
return -1;
else
return theIndex;
}
template<class T>
void arrayList<T>::erase(int theIndex) {
// 删除其索引为theIndex的元素
// 如果该元素不存在,则抛出illegal
checkIndex(theIndex);
// 有效索引,移动其索引大于theIndex的元素
copy(element + theIndex + 1, element + listSize, element + theIndex);
element[--listSize].~T(); // 调用构析函数
}
// arrayList的构造函数
template<class T>
arrayList<T>::arrayList(int initialCapacity) {
// 构造函数
if (initialCapacity < 1) {
ostringstream s;
s << "Initial capacity = " << initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
arrayLength = initialCapacity;
element = new T[arrayLength];
listSize = 0;
}
template<class T>
arrayList<T>::arrayList(const arrayList<T> &theList) {
// 复制构造函数
arrayLength = theList.arrayLength;
listSize = theList.listSize;
element = new T[arrayLength];
copy(theList.element, theList.element + listSize, element);
}
template<class T>
void arrayList<T>::insert(int theIndex, const T &theElement) {
// 在索引theIndex处插入元素theElement
if (theIndex < 0 || theIndex > listSize) {
// 无效索引
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw illegalParameterValue(s.str());
}
// 有效索引,确认数组是否已满
if (listSize == arrayLength) {
// 数组空间已满,数组长度倍增
changeLength1D(element, arrayLength, arrayLength * 2);
arrayLength *= 2;
}
// 把元素向右移动一个位置
copy_backward(element + theIndex, element + listSize, element + listSize + 1);
element[theIndex] = theElement;
++listSize;
}
// 把一个线性表插入输出流
template<class T>
void arrayList<T>::output(ostream &out) const {
copy(element, element + listSize, ostream_iterator<T>(cout, " "));
}
// 重载<<
template<class T>
ostream &operator<<(ostream &out, const arrayList<T> &x) {
x.output(out);
return out;
}
int main() {
auto *array1 = new arrayList<int>(100);
arrayList<double> y(100);
cout << *array1 << endl;
cout << y << endl;
return 0;
}
原文地址:https://www.cnblogs.com/Reion/p/16315151.html
- .NET Core开源API网关 – Ocelot中文文档
- Selenium的使用方法简介
- 爬虫代理哪家强?十大付费代理详细对比评测出炉!
- HTML5中类jQuery选择器querySelector的使用
- Ceph 集群整体迁移方案
- CSS3矩阵变换
- 可直接运行
- Ryu:网络时延探测应用
- box布局
- Event Loop
- 把图片变成字体,然后在引入到网页
- Flutter环境搭建
- java.net.ConnectException: Call From slaver1/192.168.19.128 to slaver1:8020 failed on connection exc
- 18/03/18 04:53:44 WARN TaskSchedulerImpl: Initial job has not accepted any resources; check your clu
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- redis妙用-string类型
- redis妙用-hash类型
- redis妙用-list类型
- redis妙用-set类型
- JVM调优实战:解决CMS concurrent-abortable-preclean LongGC的问题
- redis妙用-zset类型
- 【线上排查实战】AOP切面执行顺序你真的了解吗
- 使用markdown,knitr和pandoc在R语言中编写可重现的报告
- R语言广义线性模型(GLMs)算法和零膨胀模型分析
- R语言中广义线性模型(GLM)中的分布和连接函数分析
- R语言自适应平滑样条回归分析
- R语言区间数据回归分析
- R语言ggsurvplot绘制生存曲线报错 : object of type ‘symbol‘ is not subsettable
- R软件SIR模型网络结构扩散过程模拟
- R语言中使用线性模型、回归决策树自动组合特征因子水平