动态数组(Array)
Array
- 存储具有一对一逻辑关系数据的存储顺序结构。
- 数组最大的优点:快速查询,最好应用于索引有语义的情况。

插入元素
template<typename T>
bool Array<T>::add(const int index, const T& e)
{
if (index<0 || index>size)return false; //判断索引是否正确
if (size == capacity)resize(1.5 * capacity); //判断空间是否足够,不够扩容为当前的2倍
for (int i = size - 1; i >= index; --i) //从后向前循环
{
data[i + 1] = data[i]; //全部元素向后移一位
}
data[index] = e; //插入元素
++size; //数组元素的个数+1
return true;
}
扩容与缩减
template<typename T>
void Array<T>::resize(const int newcapacity)
{
T* newdata = new T[newcapacity]; //创建新的传入大小数组空间
for (int i = 0; i < size; ++i) //把原本的数组元素放入新的数组空间
{
newdata[i] = data[i];
}
delete[] data; //释放原数组的空间
data = newdata; //把原本的数组指向新的数组
newData = nullptr;
capacity = newcapacity; //新的空间大小给原本的空间大小
}
删除元素
template<typename T>
T Array<T>::remove(const int index)
{
if (index<0 || index>size)return -1; //判断索引是否正确
T ret = data[index]; //保存临时元素
for (int i = index + 1; i < size; ++i)
{
data[i - 1] = data[i]; //从要插入的元素后一位所有元素向前移一位
}
--size; //元素个数-1
if (size == capacity / 4 && capacity / 2 != 0) //判断元素个数是否为数组容量的1/4并且数组空间不为0
{
resize(capacity / 2); //调用resize传入当前容量的一半
}
return ret; //返回被删除的元素
}
时间复杂度

均摊复杂度


9次addLast操作,触发resize,总共进行了17次基本操作,平均每次addLast操作,进行2次基本操作。
假设capacity = n,n+1次addLast,触发resize,总共进行2n+1次基本操作,平均每次addLast操作进行2次基本操作。
这样均摊计算时间复杂度是O(1),同理removeLast操作,均摊复杂度也为O(1)。
代码清单
Array.h
#pragma once
#include<iostream>
template<typename T>
class Array
{
public:
//无参构造
Array() :size(0), capacity(10)
{
data = new T[10]; //默认初始化10个空间
}
//有参构造
Array(int capacity) :size(0), capacity(capacity)
{
data = new T[capacity];
}
//返回元素个数
int getSize()const;
//返回数组容量
int getCapacity()const;
//返回第一个元素
T getFirst();
//返回最后一个元素
T getLast();
//判断是否为空
bool isEmpty()const;
//插入元素
bool add(const int index, const T& e);
//头插
bool addFirst(const T& e);
//尾插
bool addLast(const T& e);
//查找数组中是否存在该元素
bool contains(const T& e)const;
//查找数组中元素的索引,不存在返回-1
int find(const T& e)const;
//删除索引元素,返回删除元素
T remove(const int index);
//删除头,返回头
T removeFirst();
//删除尾,返回尾
T removeLast();
//设置索引值
bool set(const int index, const T& e);
//获取索引值
T get(const int index)const;
//打印元素
void print()const;
//释放空间
~Array();
public:
void swap(const int i, const int j) {
if (i < 0 || i >= size || j < 0 || j >= size) {
throw Range();
}
T t = data[i];
data[i] = data[j];
data[j] = t;
}
Array(T arr[], const int n) : size(n), capacity(n) {
data = new T[n];
for (int i = 0; i < n; ++i) {
data[i] = arr[i];
}
}
private:
void resize(const int newcapacity); //重新分配空间
T* data;
int size; //数组大小
int capacity; //数组容量
};
template<typename T>
int Array<T>::getSize() const
{
return size; //返回数组长度
}
template<typename T>
int Array<T>::getCapacity() const
{
return capacity; //返回数组容量
}
template<typename T>
inline T Array<T>::getFirst()
{
return get(0);
}
template<typename T>
inline T Array<T>::getLast()
{
return get(size - 1);
}
template<typename T>
bool Array<T>::isEmpty() const
{
return size == 0; //判断是否为空,
}
template<typename T>
bool Array<T>::add(const int index, const T& e)
{
if (index<0 || index>size)return false; //判断索引是否正确
if (size == capacity)resize(2 * capacity); //判断空间是否足够,不够扩容为当前的2倍
for (int i = size - 1; i >= index; --i) //从后向前循环
{
data[i + 1] = data[i]; //全部元素向后移一位
}
data[index] = e; //插入元素
++size; //数组元素的个数+1
return true;
}
template<typename T>
bool Array<T>::addFirst(const T& e)
{
return add(0, e); //调用add从0号索引元素插入
}
template<typename T>
bool Array<T>::addLast(const T& e)
{
return add(size, e); //调用add从size号索引元素插入
}
template<typename T>
bool Array<T>::contains(const T& e) const
{
for (int i = 0; i < size; ++i) //遍历是否有查找的元素
{
if (data[i] == e)return true;
}
return false;
}
template<typename T>
int Array<T>::find(const T& e) const
{
for (int i = 0; i < size; ++i) //遍历是否有查找的元素,有返回索引,无返回-1
{
if (data[i] == e)return i;
}
return -1;
}
template<typename T>
T Array<T>::remove(const int index)
{
if (index<0 || index>size)return -1; //判断索引是否正确
T ret = data[index]; //保存临时元素
for (int i = index + 1; i < size; ++i)
{
data[i - 1] = data[i]; //从要插入的元素后一位所有元素向前移一位
}
--size; //元素个数-1
if (size == capacity / 4 && capacity / 2 != 0) //判断元素个数是否为数组容量的1/4并且数组空间不为0
{
resize(capacity / 2); //调用resize传入当前容量的一半
}
return ret; //返回被删除的元素
}
template<typename T>
T Array<T>::removeFirst()
{
return remove(0); //删除0号索引元素
}
template<typename T>
T Array<T>::removeLast()
{
return remove(size - 1); //删除size-1号索引元素
}
template<typename T>
bool Array<T>::set(const int index, const T& e)
{
if (index<0 || index>size)return false; //判断索引元素是否正确
data[index] = e; //设置索引的新元素
return true;
}
template<typename T>
T Array<T>::get(const int index) const
{
if (index<0 || index>size)return -1; //判断索引元素是否正确
return data[index]; //返回索引元素
}
template<typename T>
void Array<T>::print() const
{
std::cout << "Array:size = " << size << " Array:capacity = " << capacity << std::endl;
std::cout << "Array:data = " << "[";
for (int i = 0; i < size; ++i)
{
std::cout << data[i] << ",";
}
std::cout << "]" << std::endl;
}
template<typename T>
Array<T>::~Array()
{
delete[] data; //释放空间
data = nullptr;
}
template<typename T>
void Array<T>::resize(const int newcapacity)
{
T* newdata = new T[newcapacity]; //创建新的传入大小数组空间
for (int i = 0; i < size; ++i) //把原本的数组元素放入新的数组空间
{
newdata[i] = data[i];
}
delete[] data; //释放原数组的空间
data = newdata; //把原本的数组指向新的数组
newData = nullptr;
capacity = newcapacity; //新的空间大小给原本的空间大小
}
main.cpp
#include"Array.h"
using namespace std;
int main()
{
Array<int>* arr = new Array<int>(10);
for (int i = 0; i < 10; ++i)
{
arr->addLast(i);
}
arr->print();
cout << endl;
cout << "arr->add(1, 1)" << arr->add(1, 1) << endl;
arr->print();
cout << endl;
cout << "arr->addLast(0) " << arr->addLast(0) << endl;
arr->print();
cout << endl;
cout << "arr->addFirst(0)" << arr->addFirst(0) << endl;
arr->print();
cout << endl;
cout << "arr->contains(0)" << arr->contains(0) << endl;
arr->print();
cout << endl;
cout << "arr->find(0)" << arr->find(0) << endl;
arr->print();
cout << endl;
cout << "arr->get(1)" << arr->get(1) << endl;
arr->print();
cout << endl;
cout << "arr->set(0, 5)" << arr->set(0, 5) << endl;
arr->print();
cout << endl;
cout << "arr->getCapacity()" << arr->getCapacity() << endl;
arr->print();
cout << endl;
cout << "arr->getSize()" << arr->getSize() << endl;
arr->print();
cout << endl;
cout << "arr->isEmpty()" << arr->isEmpty() << endl;
arr->print();
cout << endl;
cout << "arr->remove(1)" << arr->remove(1) << endl;
arr->print();
cout << endl;
cout << "arr->removeFirst()" << arr->removeFirst() << endl;
arr->print();
cout << endl;
cout << "arr->removeLast()" << arr->removeLast() << endl;
arr->print();
cout << endl;
cout << "arr->getFirst" << arr->getFirst() << endl;
arr->print();
cout << endl;
cout << "arr->getLast" << arr->getLast() << endl;
arr->print();
delete arr;
return 0;
}
动态数组(Array)的更多相关文章
- python数据结构之动态数组
数组列表:动态数组(Array List) 简介: 最基础简单的数据结构.最大的优点就是支持随机访问(O(1)),但是增加和删除操作效率就低一些(平均时间复杂度O(n)) 动态数组也称数组列表,在py ...
- array of TVarRec 动态数组使用
FDQuery.AppendRecord()里是一个array of TVarRec.我们一般都是直接用[Var1,Var2,...].这样手工输入,但如果增加的元素我们预先不知道,就要声明一个arr ...
- 封装动态数组类Array
功能: 1.增.删.改.查 2.扩容.缩容 3.复杂度分析 4.均摊复杂度 5.复杂度震荡 分析动态数组的时间复杂度: 分析resize的时间复杂度: public class Array<E& ...
- DelphiXe 中静态数组TByteArray和动态数组TBytes /array of byte 的区别
在应用中发现静态数组和动态数组是有区别的: procedure TForm1.Button1Click(Sender: TObject);var RsltStream: TMemoryStream; ...
- 常用数据结构-线性表及Java 动态数组 深究
[Java心得总结六]Java容器中——Collection在前面自己总结的一篇博文中对Collection的框架结构做了整理,这里深究一下Java中list的实现方式 1.动态数组 In compu ...
- Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别
ArrayList和Vector的区别ArrayList与Vector主要从二方面来说. 一.同步性: Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...
- javascript类型系统——数组array
× 目录 [1]创建 [2]本质 [3]稀疏[4]长度[5]遍历[6]类数组 前面的话 除了对象之外,数组Array类型可能是javascript中最常用的类型了.而且,javascript中的数组与 ...
- C#中数组Array、ArrayList、泛型List<T>的比较
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...
- delphi动态数组指针问题
就一个button事件 procedure TForm1.btn7Click(Sender: TObject); Type TMyArr = array of array of array of In ...
- JS 索引数组、关联数组和静态数组、动态数组
JS 索引数组.关联数组和静态数组.动态数组 数组分类: 1.从数组的下标分为索引数组.关联数组 var ary1 = [1,3,5,8]; //按索引去取数组元素,从0开始(当然某些语言实现从1开始 ...
随机推荐
- win32-创建一个屏幕准星(UpdateLayeredWindow)
// Test_1.cpp : Defines the entry point for the application. // #include "framework.h" #in ...
- AI数字人SadTalker实战
1.概述 AI数字人在营销和品牌推广中扮演着至关重要的角色,许多企业和个人正积极利用数字技术来打造属于自己的财富.有没有一种简单而免费的方式来创建自己的数字人呢?本篇博客笔者将为大家介绍如何搭建属于自 ...
- Unity学习笔记--数据持久化Json
JSON相关 json是国际通用语言,可以跨平台(游戏,软件,网页,不同OS)使用, json语法较为简单,使用更广泛.json使用键值对来存储. 认识json文件 //注意字典类型存储时,键是以st ...
- 【小记事】如何设置vscode代码格式化时不要自动换行
最近一格式化就给我分好多行,好气哦(`ヘ´)=3 在setting.json中添加如下代码: "vetur.format.defaultFormatter.html": " ...
- Retrofit 的基本用法
一.添加依赖和网络权限 添加依赖 implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup ...
- 逆向实战31——xhs—xs算法分析
前言 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 公众号链接 目标网站 aH ...
- Go语言实现记账本
使用面向过程思想实现 package main import ( "fmt" ) func main(){ key := "" //设置初始金额 sum := ...
- instance must be started before calling this method
解决方法 检查zk的连接数: 端口号: 数据库连接配置: zk的连接配置: 如果都没有问题,就重启容器.
- 【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
问题描述 PHP的Web Job,通过artisan来配置路径启动PHP任务,相关启动脚本如下: artisan_path = "d:\\home\\site\\wwwroot"; ...
- 【Azure Developer】如何用Microsoft Graph API管理AAD Application里面的Permissions
问题描述 如何用Microsoft Graph API给应用添加Microsoft Graph Application Permission 解决方法 一:首先获取Microsoft Graph Ap ...