欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

動(dòng)態(tài)數(shù)組C++實(shí)現(xiàn)方法(分享)

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

回顧大二的數(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)方法(分享)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持我們。

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有