美文网首页
C++ 动态数组

C++ 动态数组

作者: 我阿郑 | 来源:发表于2021-12-30 19:26 被阅读0次
#ifndef ArrayList_hpp
#define ArrayList_hpp

#include <iostream>

using namespace std;
#define ELEMENT_NOT_FOUND  -1

template <class T>
class ArrayList {
public:
    ArrayList(int capacity = 10) {
        m_capacity = capacity > 10 ? capacity : 10;
        m_elements = new T[m_capacity];
    }
    ~ArrayList() {
        delete [] m_elements;
    }
    
    ArrayList<T>& add(const int index, const T& data) {
        rangeCheckForAdd(index);
        ensureCapacity(m_size + 1);
        for (int i = m_size; i > index; i--) {
            m_elements[i] = m_elements[i - 1];
        }
        m_elements[index] = data;
        m_size++;
        return *this;
    }
    
    ArrayList<T>& add(const T& data) {
        add(m_size, data);
        return *this;
    }
    
    // 把index位置的元素放入element中,然后删除index位置的元素
    // cosnt T& data , 因为这里要修改data的值,所以前面不能加const
    ArrayList<T>& remove(const int index, T& data) {
        rangeCheck(index);
        data = m_elements[index];
        for (int i = index + 1; i < m_size; i++) {
            m_elements[i - 1] = m_elements[i];
        }
        m_size--;
        m_elements[m_size] = NULL;
        return *this;
    }
    
    T& set(const int index, const T& data) const {
        rangeCheck(index);
        T& old = m_elements[index];
        m_elements[index] = data;
        return old;
    }
    
    T& get(const int index) const {
        rangeCheck(index);
        return m_elements[index];
    }
    
    // 查找某个元素,如果找到,返回元素的索引
    int indexOf(const T& data) const {
        for (int i = 0; i < m_size; i++) {
            if (m_elements[i] == data) {
                return i;
            }
        }
        return ELEMENT_NOT_FOUND;
    }
    
    bool isEmpty() const{
        return m_size == 0;
    }
    
    int size() const {
        return m_size;
    }
    
    ArrayList<T>& clear() {
        for (int i = 0; i < m_size; i++) {
            m_elements[i] = NULL;
        }
        m_size = 0;
        return *this;
    }
    
    void output(ostream& out) const {
        for (int i = 0; i < m_size; i++) {
            out << m_elements[i] << ", ";
        }
    }
    
private:
    int m_size;
    int m_capacity;
    T *m_elements;
    
    void outOfBounds(const int index) const {
        throw "out of bounds";
    }
    void rangeCheck(const int index) const {
        if (index < 0 || index >= m_size) {
            outOfBounds(index);
        }
    }
    void rangeCheckForAdd(const int index) const {
        if (index < 0 || index > m_size) {
            outOfBounds(index);
        }
    }
    
    // void ensureCapacity(int capacity) const
    // 因为这个函数里面会修改成员变量 m_elements 和 m_capacity 的值,所以不能用const修饰了
    void ensureCapacity(const int capacity) {
        int oldCapacity = m_capacity;
        if (oldCapacity >= capacity) {
            return;
        }
        // 新容量为旧容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        T *newElements = new T[newCapacity];
        // 拷贝数据
        for (int i = 0; i < m_size; i++) {
            newElements[i] = m_elements[i];
        }
        delete [] m_elements;
        m_elements = newElements;
        m_capacity = newCapacity;
        cout << oldCapacity << "扩容为" << newCapacity << endl;
    }
};

// 重载 <<
template <class T>
ostream& operator<<(ostream& out, const ArrayList<T>& list) {
    list.output(out);
    return out;
}

#endif /* ArrayList_hpp */

测试

int main(int argc, const char * argv[]) {
    ArrayList<int> array;
    array.add(11).add(22).add(33).add(55);
    array.add(2, 44);
    array.set(3, 66);
    int e = 0;
    array.remove(1, e);
    cout << "array size is " << array.size() << endl;
    cout << "delete element is " << e << endl;
    
    cout << "List is: " << array << endl;
    
    array.clear();
    cout << "after clear() array size is " << array.size() << endl;

    return 0;
}

相关文章

  • C++知识查缺补漏

    C++知识查缺补漏 Vector 向量,动态数组 C++中的一个类,用起来相当于动态数组,可以使用在一开始无法确定...

  • c++ new 和delete的种种

    new/delete动态管理对象,new[]/delete[]动态管理对象数组。 C++中,把int 、char....

  • c++primer 12.23-12.33

    12.23这个题要注意c++对于char的几个常用的方法 12.24这个题要清楚,只要申请了动态数组,动态数组会根...

  • C++动态数组

    在C++中有的时候定义一个数组的长度大小是由用户自己定义的,这个时候就需要声明动态数组。 声明一维动态数组: 声明...

  • c++动态数组

    #include #include //动态增长内存,策略将存放数据的内存放到那?堆上 //动态数组如果5个元素申...

  • C++动态数组

  • C++ 动态数组

    测试

  • C实现的vector动态数组

    动态数组vector是日常业务代码最常用的数据结构,大多数高级语言都提供了动态数组的实现, 如c++中的std::...

  • C、C++之动态数组的实现二(C++版本)

    c、c++动态数组(c++版本) 本篇文章基于笔者正在参与的c++课程,第二次作业的内容是要求使用c++的特性对上...

  • C++定义动态数组

    为什么需要动态定义数组呢? 很多情况下,在预编译过程阶段,数组的长度是不能预先知道的,必须在程序运行时动态的给出。...

网友评论

      本文标题:C++ 动态数组

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