C++模拟数组

作者: 锋芒不露大宝剑 | 来源:发表于2019-03-25 01:07 被阅读0次

使用C++写一个类似NSArray的数组, 欢迎指正.

```

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;

}

```

相关文章

网友评论

    本文标题:C++模拟数组

    本文链接:https://www.haomeiwen.com/subject/cydmvqtx.html