【Play-with-Data-Structures-master】---part1: chapter1(介绍)+chapter2(数组)

本文介绍了一个使用C++实现的动态数组类,该类能够自动调整容量以适应元素的增删操作,并提供了丰富的成员函数来方便地进行数组操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引用: https://blog.youkuaiyun.com/LKJgdut/article/details/104956018
https://blog.youkuaiyun.com/LKJgdut/article/details/104956018
代码C++实现:

#include <iostream>
#include <cassert>

template<typename T>
class Array {
private:
    T *data;
    int size;
    int capacity;

    // 将数组空间的容量变成newCapacity大小
    void resize(int newCapacity) {
        T *newData = new T[newCapacity];
        for (int i = 0; i < size; ++i) {
            newData[i] = data[i];
        }
        data = newData;
        capacity = newCapacity;
        newData = nullptr;
        delete []newData;
    }
public:
    // 构造函数,传入数组的容量capacity构造Array
    Array(int capacity) {
        data = new T[capacity];
        size = 0;
        this->capacity = capacity;
    }

    // 无参数的构造函数,默认数组的容量capacity=5
    Array() {
        data = new T[5];
        size = 0;
        capacity = 5;
    }

    // 获取数组的容量
    int getCapacity() {
        return capacity;
    }

    // 获取数组中的元素个数
    int getSize() {
        return size;
    }

    // 返回数组是否为空
    bool isEmpty() {
        return size == 0;
    }

    // 在index索引的位置插入一个新元素e
    void add(int index, T e) {
        assert(index >= 0 && index <= size);
        if (size == capacity) {
            resize(2 * capacity);
        }
        for (int i = size - 1; i >= index; --i) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    // 在所有元素前添加一个新元素
    void addFirst(T e) {
        add(0, e);
    }

    // 向所有元素后添加一个新元素
    void addLast(T e) {
        add(size, e);
    }

    // 获取index索引位置的元素
    T get(int index) {
        assert(index >= 0 && index < size);
        return data[index];
    }

    // 修改index索引位置的元素为e
    void set(int index, T e) {
        assert(index >= 0 && index < size);
        data[index] = e;
    }

    // 查找数组中是否有元素e
    bool contains(T e) {
        for (int i = 0; i < size; ++i) {
            if (data[i] == e) {
                return true;
            }
        }
        return false;
    }

    // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
    int find(T e) {
        for (int i = 0; i < size; ++i) {
            if (data[i] == e) {
                return i;
            }
        }
        return -1;
    }

    // 从数组中删除index位置的元素, 返回删除的元素
    T remove(int index) {
        assert(index >= 0 && index < size);
        T ret = data[index];
        for (int i = index + 1; i < size; ++i) {
            data[i - 1] = data[i];
        }
        size--;//注意维护size
        if (size == capacity / 4 && capacity / 2 != 0) {//capacity / 4:lazy方式
            resize(capacity / 2);
        }
        return ret;
    }

    // 从数组中删除第一个元素, 返回删除的元素
    T removeFirst() {
        return remove(0);
    }

    // 从数组中删除最后一个元素, 返回删除的元素
    T removeLast() {
        return remove(size - 1);
    }

    // 从数组中删除元素e
    void removeElement(T e) {
        int index = find(e);
        if (index != -1) {
            remove(index);
        }
    }

    //打印数组的所有元素
    void print() {
        std::cout << "Array: size = " << size << ", capacity = " << getCapacity() << std::endl;
        std::cout << "[";
        for (int i = 0; i < size; ++i) {
            std::cout << data[i];
            if (i != size - 1) {
                std::cout << ",";
            }
        }
        std::cout << "]";
	    std::cout << std::endl;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值