c++ - 如何创建通用动态数组?

标签 c++ arrays

你好,我有这个 IntArray 我想转换为接受泛型类型:

class IntArray {

private:
    int* first_cell = nullptr;
    int size = 0; // currently occupied elements
    int capacity = 8; // size of the allocated memory

public:
    IntArray() {
        first_cell = new int[capacity]; // Declare the array in memory
    }

    IntArray(const IntArray& src)
        : size(src.size),
        capacity(src.capacity)
    {
        first_cell = new int[capacity];
        std::copy_n(src.first_cell, size, first_cell);
    }

    IntArray(IntArray&& src)
        : first_cell(src.first_cell),
        size(src.size),
        capacity(src.capacity)
    {
        src.first_cell = nullptr;
        src.size = src.capacity = 0;
    }

    ~IntArray() {
        delete[] first_cell;
    }

    IntArray& operator=(IntArray rhs) {
        IntArray temp(std::move(rhs));
        std::swap(first_cell, temp.first_cell);
        std::swap(size, temp.size);
        std::swap(capacity, temp.capacity);
        return *this;
    }

    void push_back(int number) {
        if (size == capacity) {
            int new_cap = capacity * 2; // increased capacity
            int* new_arr = new int[new_cap]; // new arr with new capacity

            for (int k = 0; k < size; ++k) {
                new_arr[k] = first_cell[k]; // copy data from frist array
            }

            delete[] first_cell; // remove first array

            first_cell = new_arr;
            capacity = new_cap;
        }
        first_cell[size] = number;
        ++size;
    }

    int length() {
        return size;
    }

    int index_of(int number) {
        for (int k = 0; k < size; k++) {
            
            if (number == first_cell[k]) {
                
                return k;
            }           
        }
        return -1;
    }



    void print(char symb) {
        for (int k = 0; k < size; ++k) {            
            std::cout << first_cell[k] << symb;
        }
    }
};
我如何将这个数组转换为接受任何类型,如泛型数组?我希望能够接受任何类型,这可能吗?我如何检查类型?如果你能指出我正确的方向,因为我不知道怎么做?谢谢。

最佳答案

我想到了;

template<class T>
class List {

private:
    T* first_cell = nullptr;
    int size = 0; // currently occupied elements
    int capacity = 8; // size of the allocated memory

public:
    List() {
        first_cell = new T[capacity]; // Declare the array in memory
    }

    List(const List& src)
        : size(src.size),
        capacity(src.capacity)
    {
        first_cell = new T[capacity];
        std::copy_n(src.first_cell, size, first_cell);
    }

    List(List&& src)
        : first_cell(src.first_cell),
        size(src.size),
        capacity(src.capacity)
    {
        src.first_cell = nullptr;
        src.size = src.capacity = 0;
    }

    ~List() {
        delete[] first_cell;
    }

    List& operator=(List rhs) {
        List temp(std::move(rhs));
        std::swap(first_cell, temp.first_cell);
        std::swap(size, temp.size);
        std::swap(capacity, temp.capacity);
        return *this;
    }

    void push_back(int number) {
        if (size == capacity) {
            int new_cap = capacity * 2; // increased capacity
            T* new_arr = new T[new_cap]; // new arr with new capacity

            for (int k = 0; k < size; ++k) {
                new_arr[k] = first_cell[k]; // copy data from frist array
            }

            delete[] first_cell; // remove first array

            first_cell = new_arr;
            capacity = new_cap;
        }
        first_cell[size] = number;
        ++size;
    }

    int length() {
        return size;
    }

    int first_index_of(int number) {
        for (int k = 0; k < size; k++) {
            
            if (number == first_cell[k]) {
                
                return k;
            }           
        }
        return -1;
    }



    void print(char symb) {
        for (int k = 0; k < size; ++k) {            
            std::cout << first_cell[k] << symb;
        }
    }
};

关于c++ - 如何创建通用动态数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65676785/

相关文章:

c++ - 合并几个 boost 序列化的 OpenCV Mats

c - 读入一个包含 5 行的文本文件,并将其填充到 C 中的二维数组中

arrays - 为 foreach 循环中的每个循环创建一个新变量

c++ - 将数组指针访问从 C++ 转换为 Delphi

c# - 如何跨多维数组(linq 或简单算法)中的列运行逻辑 'xnor'?

arrays - 如何根据matlab中另一个矩阵的行大小 reshape 列矩阵

c++ - Word Solver - 所有方向

c++ numerical analysis 精确的数据结构?

c++ - 将 OGRE::Image 转换为 cv::Mat

c++ - 从 C++ 程序打开两个 wxt 终端