動態(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)
更新時間:2017年05月24日 08:34:42 投稿:jingxian
下面小編就為大家?guī)硪黄獎討B(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
回顧大二的數(shù)據(jù)結(jié)構(gòu)知識。從數(shù)組開始。實(shí)現(xiàn)了一個可自動擴(kuò)充容量的泛型數(shù)組。
頭文件:Array.h
#ifndef Array_hpp #define Array_hpp template <class T> class Array{ private: T *base; //數(shù)組首地址 int length; //數(shù)組中元素 int size; //數(shù)組大小,以數(shù)組中元素的大小為單位 public: //初始化數(shù)組,分配內(nèi)存 bool init(); //檢查內(nèi)存是否夠用,不夠用就增加 bool ensureCapcity(); //添加元素到數(shù)組尾 bool add(T item); //插入元素到數(shù)組的具體位置,位置從1開始 bool insert(int index,T item); //刪除指定位置的元素并返回,位置從1開始 T del(int index); //返回指定位置的元素 T objectAt(int index); //打印數(shù)組所有元素 void display(); }; #endif /* Array_hpp */
實(shí)現(xiàn):Array.cpp
#include "Array.hpp" #include <mm_malloc.h> #include <iostream> using namespace std; template<typename T> bool Array<T>::init(){ base = (T *)malloc(10*sizeof(T)); if(!base){ return false; } size = 10; length = 0; return true; } template<typename T> bool Array<T>::ensureCapcity(){ if(length >= size){ T *newBase = (T*)realloc(base,10 * sizeof(T) + size); if(!newBase){ return false; } base = newBase; size += 10; newBase = nullptr; } return true; } template<typename T> bool Array<T>::add(T item){ if(!ensureCapcity()){ return false; } T *p = base + length; *p = item; length ++; return true; } template<typename T> bool Array<T>::insert(int index,const T item){ if(!ensureCapcity()){ return false; } if(index < 1 || index > length){ return false; } T *q = base + index - 1; T *p = base + length - 1; while( p >= q){ *(p+1) = *p; p--; } *q = item; q = nullptr; p = nullptr; length ++; return true; } template<typename T>T Array<T>::del(int index){ if(index<1 || index > length){ return NULL; } T *q = base + index - 1; T item = *q; ++q; T *p = base + length; while(q <= p){ *(q-1)=*q; ++q; } length --; return item; } template<typename T>T Array<T>::objectAt(int index){ if(index<1 || index > length){ return NULL; } T *q = base; return *(q + index - 1); } template <typename T>void Array<T>::display(){ T *q = base; T *p = base +length - 1; while (q<=p) { cout << *(q++)<<" "; } cout << endl; }
使用:
#include <iostream> #include "Array.cpp" using namespace std; int main(int argc, const char * argv[]) { Array<int> array = *new Array<int>; array.init(); array.add(1); array.insert(1,2); array.objectAt(1); return 0; }
以上這篇動態(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C++線性表深度解析之動態(tài)數(shù)組與單鏈表和棧及隊(duì)列的實(shí)現(xiàn)
- C++ 動態(tài)數(shù)組模版類Vector實(shí)例詳解
- C++ Vector 動態(tài)數(shù)組的實(shí)現(xiàn)
- C++實(shí)現(xiàn)動態(tài)數(shù)組功能
- c++創(chuàng)建二維動態(tài)數(shù)組與內(nèi)存釋放問題
- C/C++ 動態(tài)數(shù)組的創(chuàng)建的實(shí)例詳解
- 淺談C++內(nèi)存分配及變長數(shù)組的動態(tài)分配
- C++動態(tài)數(shù)組類的封裝實(shí)例
- C++詳解如何實(shí)現(xiàn)動態(tài)數(shù)組
相關(guān)文章
windows 下C++生成Dump調(diào)試文件與分析
dump文件是C++程序發(fā)生異常時,保存當(dāng)時程序運(yùn)行狀態(tài)的文件,是調(diào)試異常程序重要的方法,所以程序崩潰時,除了日志文件,dump文件便成了我們查找錯誤的最后一根救命的稻草,這篇文章主要介紹了windows 下C++生成Dump調(diào)試文件與分析,需要的朋友可以參考下2023-04-04C語言數(shù)據(jù)結(jié)構(gòu)算法基礎(chǔ)之循環(huán)隊(duì)列示例
這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)算法基礎(chǔ)之循環(huán)隊(duì)列,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06