動(dòng)態(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)
更新時(shí)間:2017年05月24日 08:34:42 投稿:jingxian
下面小編就為大家?guī)硪黄獎(jiǎng)討B(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
回顧大二的數(shù)據(jù)結(jié)構(gòu)知識(shí)。從數(shù)組開始。實(shí)現(xiàn)了一個(gè)可自動(dòng)擴(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;
}
以上這篇?jiǎng)討B(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C++線性表深度解析之動(dòng)態(tài)數(shù)組與單鏈表和棧及隊(duì)列的實(shí)現(xiàn)
- C++ 動(dòng)態(tài)數(shù)組模版類Vector實(shí)例詳解
- C++ Vector 動(dòng)態(tài)數(shù)組的實(shí)現(xiàn)
- C++實(shí)現(xiàn)動(dòng)態(tài)數(shù)組功能
- c++創(chuàng)建二維動(dòng)態(tài)數(shù)組與內(nèi)存釋放問題
- C/C++ 動(dòng)態(tài)數(shù)組的創(chuàng)建的實(shí)例詳解
- 淺談C++內(nèi)存分配及變長數(shù)組的動(dòng)態(tài)分配
- C++動(dòng)態(tài)數(shù)組類的封裝實(shí)例
- C++詳解如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)組
相關(guān)文章
C++實(shí)現(xiàn)動(dòng)態(tài)分配const對(duì)象實(shí)例
這篇文章主要介紹了C++實(shí)現(xiàn)動(dòng)態(tài)分配const對(duì)象實(shí)例,包括了const對(duì)象的創(chuàng)建、刪除及應(yīng)用實(shí)例,需要的朋友可以參考下2014-10-10
淺談c++構(gòu)造函數(shù)問題,初始化和賦值問題
下面小編就為大家?guī)硪黄獪\談c++構(gòu)造函數(shù)問題,初始化和賦值問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
Qt編寫地圖之實(shí)現(xiàn)經(jīng)緯度坐標(biāo)糾偏
地圖應(yīng)用中都涉及到一個(gè)問題就是坐標(biāo)糾偏的問題,這個(gè)問題的是因?yàn)楦鶕?jù)地方規(guī)則保密性要求不允許地圖廠商使用標(biāo)準(zhǔn)的GPS坐標(biāo),而是要用國家定義的偏移標(biāo)準(zhǔn)。本文將詳細(xì)講解如何在Qt中實(shí)現(xiàn)經(jīng)緯度坐標(biāo)糾偏,需要的可以參考一下2022-03-03

