C++模擬數(shù)組

使用C++寫一個類似NSArray的數(shù)組, 歡迎指正.

```

hpp

#ifndef home_int_array_hpp

#define home_int_array_hpp

#include <iostream>

#include <string.h>

using namespace std;

class IntArray {

? ? friend ostream & operator<< (ostream &os, IntArray const &array);

public:

? ? IntArray();

? ? IntArray(int size);

? ? ~IntArray();

? ? void addObject(int obj);

? ? void insertObject(int idx, int obj);

? ? int objectAtIndex(unsigned int index);

? ? int & operator[] (int idx);

? ? bool operator== (IntArray const &other);

? ? bool operator!= (IntArray const &other);

private:

? ? int _index;

? ? int *_space;

protected:

? ? int init_vessel(int size);

? ? int _size;

};

extern ostream & operator<< (ostream &os, IntArray const &array);

#endif /* home_int_array_hpp */

cpp

#include "home_int_array.hpp"

IntArray::IntArray() {

? ? _index = 0;

? ? _size = 0;

? ? this->_space = NULL;

}

IntArray::IntArray(int size): _size(size) {

? ? _index = 0;

? ? _space = NULL;

? ? init_vessel(size);

}

IntArray::~IntArray() {

? ? if (NULL != _space) {

? ? ? ? delete _space;

? ? ? ? _space = NULL;

? ? }

? ? _index = 0;

? ? _size = 0;

}

void IntArray::addObject(int obj) {

? ? if (_space == NULL) {

? ? ? ? init_vessel(_size==0?1:_size);

? ? }

? ? if (_size < (_index + 1)) {

? ? ? ? init_vessel(_size * 2);

? ? }

? ? _space[_index] = obj;

? ? _index++;

}

void IntArray::insertObject(int idx, int obj) {

? ? if (idx > _index) {

? ? ? ? cout << "越界" << endl;

? ? ? ? return;

? ? }

? ? if (_space == NULL) {

? ? ? ? init_vessel(_size==0?1:_size);

? ? }

? ? if (_size < (_index + 1)) {

? ? ? ? init_vessel(_size * 2);

? ? }

? ? int *tmp = _space + idx;

? ? memcpy(_space + idx + 1, tmp, sizeof(int) * (_index - idx));

? ? _space[idx] = obj;

? ? _index++;

}

int IntArray::objectAtIndex(unsigned int index) {

? ? if (_space == NULL) {

? ? ? ? return 0;

? ? }

? ? if (index > _index) {

? ? ? ? cout << "越界" << endl;

? ? ? ? return 0;

? ? }

? ? return *(_space + index);

}

int IntArray::init_vessel(int size) {

? ? _size = size;

? ? if (_size > 0) {

? ? ? ? if (_space != NULL) {

? ? ? ? ? ? int *tmp = new int[_size];

? ? ? ? ? ? memset(tmp, 0, sizeof(int) * _size);

? ? ? ? ? ? memcpy(tmp, _space, (_index + 1) * sizeof(int));

? ? ? ? ? ? delete _space;

? ? ? ? ? ? _space = NULL;

? ? ? ? ? ? _space = tmp;

? ? ? ? } else {

? ? ? ? ? ? this->_space = new int[_size];

? ? ? ? ? ? memset(this->_space, 0, sizeof(int) * _size);

? ? ? ? }

? ? }

? ? return 0;

}

ostream & operator<< (ostream &os, IntArray const &array) {

? ? int i = 0;

? ? os << "長度(" << array._size << ") : ";

? ? for (i = 0; i < array._index; i++) {

? ? ? ? os << array._space[i] << ", ";

? ? }

? ? return os;

}

int & IntArray::operator[] (int idx) {

? ? cout << idx << endl;

? ? return *(_space + idx);

}

bool IntArray::operator== (IntArray const &other) {

? ? if (other._index != this->_index) {

? ? ? ? return false;

? ? }

? ? int i = 0;

? ? for (i = 0; i < _index; i++) {

? ? ? ? if (*(this->_space + i) != *(other._space + i)) {

? ? ? ? ? ? return false;

? ? ? ? }

? ? }


? ? return true;

}

bool IntArray::operator!= (IntArray const &other) {

? ? if (other._index != this->_index) {

? ? ? ? return true;

? ? }

? ? int i = 0;

? ? for (i = 0; i < _index; i++) {

? ? ? ? if (*(this->_space + i) != *(other._space + i)) {

? ? ? ? ? ? return true;

? ? ? ? }

? ? }

? ? return false;

}

```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容