两个版本:
具体类型(int[])和 (short/int/long/float/double[])通用的模板类型
先介绍一个获取 new 和 malloc 动态分配 数组大小的函数:
_msize() 返回 new和malloc 分配内存的大小
返回类型 size_t 是这样定义的:
#define __SIZE_TYPE long unsigned int
typedef __SIZE_TYPE__ size_t;
即size_t实际上是无符号长整型,在32位系统上位32位,在64位系统中位64位
一般返回某个类型的数据所占用的内存首先是考虑 sizeof() 和 strlen() 函数,但对于用 new 分配出来的内存来说如:char * pc = new char[100] ; 想返回 pc 所指向的内存的大小,sizeof(pc), 或strlen(pc) 都不能得到正确的结果,因为 sizeof() 返回的是指针本身所占用内存的大小,strlen() 只能用char*做参数,且必须是以''\0''结尾的。
而_msize() 函数则可以返回用 new 方式分配出来的内存大小,如下例:
char * pc = new char[100] ;
size_t size = _msize(pc) / sizeof(char);
最后结果是 size = 100。
具体的实现原理,等有时间在补充…
balabalabala…
具体类型(int[])的代码:
头文件: dArrays.h
#ifndef DARRAY_DARRAYS_H
#define DARRAY_DARRAYS_H
class DAarray{
private:
int size;
const int CAPACITY = 10;
int *arr;
void capacityEnough(int capacity);
public:
DAarray();
explicit DAarray(int capacity);
~DAarray();
int Size() const;
bool IsEmpty() const{
return size != 0;
}
void insert(int elems);
void insert(int elems, int index);
int Delete(int index);
int reset(int index, int elems);
int getElement(int index) const;
int getIndexOfElement(int elems) const;
void display() const;
};
#endif //DARRAY_DARRAYS_H
头文件实现代码:dArrays.cpp
#include "dArrays.h"
#include <iostream>
DAarray::DAarray(){
size = 0;
arr = new int[CAPACITY]{0};
}
// 此处 若不初始化 size=0, size 会被赋予 随机值,给我的随机值为 8,必须要初始化。这和 java 不同,java 默认为 0
DAarray::DAarray(int capacity): size(0){
int cap = capacity>CAPACITY? capacity:CAPACITY;
arr = new int[cap]{0};
}
DAarray::~DAarray() {
delete[] arr;
}
void DAarray::capacityEnough(int capacity) {
size_t oldCap = _msize(arr) / sizeof(int);
if (oldCap >= capacity) return;
// 扩容为 原来内存的 1.5 倍
size_t newCap = oldCap + (oldCap >> 1);
int* parr = new int[newCap]{0};
for (int i=0; i<oldCap; i++){
parr[i] = arr[i];
}
int* temp = arr;
arr = parr;
delete[] temp;
//std::cout << oldCap << "--->" << newCap << std::endl;
}
void DAarray::insert(int elems){
insert(elems, size);
}
void DAarray::insert(int elems, int index){
capacityEnough(size + 1);
for (int i = size - 1; i >= index; i--){
arr[i+1] = arr[i];
}
arr[index] = elems;
++size;
}
int DAarray::Delete(int index) {
int oldElemet = arr[index];
for(int i=index; i < size; ++i){
arr[i] = arr[i+1];
}
--size;
return oldElemet;
}
int DAarray::reset(int index, int elems) {
int oldElement = arr[index];
arr[index] = elems;
return oldElement;
}
int DAarray::Size() const {
return size;
}
int DAarray::getElement(int index) const {
return arr[index];
}
int DAarray::getIndexOfElement(int elems) const {
for(int i = 0; i < size; i++){
if (arr[i] == elems){
return i;
}
}
return -1;
}
void DAarray::display() const {
std::cout << "[ ";
for(int i=0; i < size; ++i){
std::cout << arr[i] << " ";
}
std::cout << "]" << std::endl;
}
测试代码:main.cpp
#include <iostream>
#include "dArrays.h"
using std::cout;
using std::endl;
int main(){
DAarray arrs = DAarray(5);
for (int i=0; i<10; i++){
arrs.insert(i*2);
}
cout << "size=" << arrs.Size() << endl;
cout << "is empty: " << arrs.IsEmpty() << endl;
arrs.display();
cout << "------------------------------------------------" << endl;
// 获取指定索引的 元素
int element = arrs.getElement(3);
cout << "index=3----->" << element <<endl;
cout << "------------------------------------------------" << endl;
// 获取指定元素的索引
int index = arrs.getIndexOfElement(10);
cout << "10-->index= " << index << endl;
cout << "------------------------------------------------" << endl;
// 修改指定索引处的元素
element = arrs.reset(5, 200);
cout << "old element: " << element << endl;
cout << "------------------------------------------------" << endl;
// 删除指定索引处的 元素
element = arrs.Delete(3);
cout << "delete element: " << element <<endl;
cout << "------------------------------------------------" << endl;
// 任意位置插入元素
arrs.insert(100, 0); // 头处 插入元素
arrs.insert(200, 0);
arrs.insert(300, 0);
arrs.insert(1000); // 结尾插入元素
arrs.insert(5000);
// 中插入元素
for (int i=0; i < 10; ++i){
arrs.insert(i*9, i+3);
}
cout << "size=" << arrs.Size() << endl;
arrs.display();
cout << "------------------------------------------------" << endl;
return 0;
}
输出结果:
size=10
Not Empty: 1
[ 0 2 4 6 8 10 12 14 16 18 ]
------------------------------------------------
index=3----->6
------------------------------------------------
10-->index= 5
------------------------------------------------
old element: 10
------------------------------------------------
delete element: 6
------------------------------------------------
size=24
[ 300 200 100 0 9 18 27 36 45 54 63 72 81 0 2 4 8 200 12 14 16 18 1000 5000 ]
------------------------------------------------
Process finished with exit code 0
模板类型的动态数组实现
代码:头文件:dArrays.h
#ifndef DARRAY_DARRAYS_H
#define DARRAY_DARRAYS_H
#include <iostream>
template <class T>
class DAarray {
private:
int size;
const int CAPACITY = 10;
T *arr;
void capacityEnough(int capacity);
public:
DAarray();
explicit DAarray(int capacity);
~DAarray();
int Size() const;
inline bool IsEmpty() const {
return size != 0;
}
void insert(T elems);
void insert(T elems, int index);
T Delete(int index);
T reset(int index, T elems);
T getElement(int index) const;
int getIndexOfElement(T elems) const;
void display() const;
};
// 下面为 模板的具体实现代码
template <class T>
DAarray<T>::DAarray(){
size = 0;
arr = new T[CAPACITY]{T()}; // T() 表示:使用具体类型的 0 值初始化数组
}
// 此处 若不初始化 size=0, size 会被赋予 随机值,给我的随机值为 8,必须要初始化。这和 java 不同,java 默认为 0
template <class T>
DAarray<T>::DAarray(int capacity): size(0){
int cap = capacity>CAPACITY? capacity:CAPACITY;
arr = new T[cap]{T()};
}
template<class T>
DAarray<T>::~DAarray() {
delete[] arr;
}
template<class T>
void DAarray<T>::capacityEnough(int capacity) {
int oldCap = _msize(arr) / sizeof(T);
if (oldCap >= capacity) {
return;
}
// 扩容为 原来内存的 1.5 倍
int newCap = oldCap + (oldCap >> 1);
T* parr = new T[newCap]{T()};
for (int i=0; i<oldCap; i++){
parr[i] = arr[i];
}
T* temp = arr;
arr = parr;
delete[] temp;
// std::cout << oldCap << "--->" << newCap << std::endl;
}
template<class T>
void DAarray<T>::insert(T elems){
insert(elems, size);
}
template <class T>
void DAarray<T>::insert(T elems, int index){
capacityEnough(size + 1);
for (int i = size - 1; i >= index; i--){
arr[i+1] = arr[i];
}
arr[index] = elems;
++size;
}
template <class T>
T DAarray<T>::Delete(int index) {
int oldElemet = arr[index];
for(int i=index; i < size; ++i){
arr[i] = arr[i+1];
}
--size;
return oldElemet;
}
template <class T>
T DAarray<T>::reset(int index, T elems) {
int oldElement = arr[index];
arr[index] = elems;
return oldElement;
}
template <class T>
int DAarray<T>::Size() const {
return size;
}
template <class T>
T DAarray<T>::getElement(int index) const {
return arr[index];
}
template <class T>
int DAarray<T>::getIndexOfElement(T elems) const {
for(int i = 0; i < size; i++){
if (arr[i] == elems){
return i;
}
}
return -1;
}
template <class T>
void DAarray<T>::display() const {
std::cout << "[ ";
for(int i=0; i < size; ++i){
std::cout << arr[i] << " ";
}
std::cout << "]" << std::endl;
}
#endif //DARRAY_DARRAYS_H
测试代码:main.cpp
#include <iostream>
#include "dArrays.h"
using std::cout;
using std::endl;
typedef int TT;
int main(){
DAarray<TT> arrs = DAarray<TT>(5);
for (int i=0; i<10; i++){
arrs.insert(i*2);
}
cout << "size=" << arrs.Size() << endl;
cout << "Not Empty: " << arrs.IsEmpty() << endl;
arrs.display();
cout << "------------------------------------------------" << endl;
// 获取指定索引的 元素
int element = arrs.getElement(3);
cout << "index=3----->" << element <<endl;
cout << "------------------------------------------------" << endl;
// 获取指定元素的索引
int index = arrs.getIndexOfElement(10);
cout << "10-->index= " << index << endl;
cout << "------------------------------------------------" << endl;
// 修改指定索引处的元素
element = arrs.reset(5, 200);
cout << "old element: " << element << endl;
cout << "------------------------------------------------" << endl;
// 删除指定索引处的 元素
element = arrs.Delete(3);
cout << "delete element: " << element <<endl;
cout << "------------------------------------------------" << endl;
// 任意位置插入元素
arrs.insert(100, 0); // 头处 插入元素
arrs.insert(200, 0);
arrs.insert(300, 0);
arrs.insert(1000); // 结尾插入元素
arrs.insert(5000);
// 中插入元素
for (int i=0; i < 10; ++i){
arrs.insert(i*9, i+3);
}
cout << "size=" << arrs.Size() << endl;
arrs.display();
cout << "------------------------------------------------" << endl;
// char ll = 65;
// cout << ll << endl;
return 0;
}
测试结果:
size=10
Not Empty: 1
[ 0 2 4 6 8 10 12 14 16 18 ]
------------------------------------------------
index=3----->6
------------------------------------------------
10-->index= 5
------------------------------------------------
old element: 10
------------------------------------------------
delete element: 6
------------------------------------------------
size=24
[ 300 200 100 0 9 18 27 36 45 54 63 72 81 0 2 4 8 200 12 14 16 18 1000 5000 ]
------------------------------------------------
Process finished with exit code 0