带有for-each支持c ++的自定义列表

时间:2015-03-31 22:59:19

标签: c++ loops arraylist foreach custom-lists

一开始我想为我的英语道歉。我的问题很奇怪,因为我正在尝试编写自己的ArrayList外观,并且像Java中的List一样工作,我知道它就像重新发明一个轮子,但我这样做是为了好玩,更好地理解它是如何工作的。因此,每个答案,如“使用STL”,“使用向量”,“使用其他东西但不创建自己的列表”都没有用。 所以从一开始,我有自己的模板到ArrayList,有一些对我很重要的方法。我的ArrayList作为数组工作,我只使用一部分可用空间,如果我需要更多空间,我创建新的更大的数组并复制所有旧元素(我相信它在Java ArrayList中的工作方式,告诉我,如果我错了) 。在数组中,我只存储指针。它是保存和删除已知对象的好模板,如果我只需要读取存储的数据,但是当我尝试写入循环进行读取时,会出现真正的问题,检查并从列表中删除指定的元素。我不能使用标准来做(int i = 0; i

所以有人可以解释我应该在我的模板中写什么来支持每个功能。在许多示例中出现begin()和end()函数,但没有人解释为什么这个名称,什么是返回类型和原因,以及此方法应该返回什么。这是我的模板代码,如果出现问题,请告诉我。我将在我的其他应用程序中使用此代码,因为对我来说,这个实现比矢量更直观(我肯定用Java工作太长了)))

template <class T> class ArrayList {

public:
    ArrayList() {
        array = new T*[1000];
        arraySize = 1000;
        n = 0;
    };

    void add(T &arg) { //add new element at end
        if (n == arraySize) {
            increase();
        }
        array[n] = &arg;
        n++;
    };

    void addAt(T &arg, unsigned int pos) { //add new element at specific position and override
        if (pos >= 0 && pos <= n) {
            if (pos == n) {
                add(arg);
            }
            else {
                array[pos] = &arg;
            }
        }
        else {
            throw "IndexOutOfBoundException";
        }
    };

    void addAfter(T &arg, unsigned int pos) { //add new element between specific posittion and next element
        pos++;
        if (pos >= 0 && pos <= n) {
            if (n == arraySize) {
                increase();
            }
            for (unsigned int i = n; i > pos; i--) {
                array[i] = array[i - 1];
            }
            array[pos] = &arg;
            n++;
        }
        else {
            throw "IndexOutOfBoundException";
        }
    };

    void addList(ArrayList &list) { //add 'list' at the end
        if (list.n > 0) {
            while (list.n + n > arraySize) {
                increase();
            }
            for (int i = 0; i < list.n; i++) {
                array[n] = list.array[i];
                n++;
            }
        }
    };

    void addListAfter(ArrayList &list, unsigned int pos) { //put 'list' inside list, start from 'pos'
        pos++;
        if (list.n > 0 && pos >= 0 && pos < n) {
            while (list.n + n > arraySize) {
                increase();
            }
            int m = n - 1;
            while (m >= pos && m >= 0) {
                array[m + list.n] = array[m];
                m--;
            }
            for (int i = 0; i < list.n; i++) {
                array[pos + i] = list.array[i];
            }
            n += list.n;
        }
        else {
            throw "IndexOutOfBoundException";
        }
    };

    void addListAfter(ArrayList &list, T &arg) { //put 'list' inside list, start after T, if T not exist 'list' will be added at the end
        addListAfter(list, getIndex(arg));
    };

    void remove(T &arg, bool all) { //remove selected element if all=true remove all instance of object otherwise remove only first
        if (all) {
            int copies = 0;
            for (int index = 0; index < n; index++) {
                if (array[index] == &arg) {
                    copies++;
                }
                else if (copies != 0) {
                    array[index - copies] = array[index];
                }
            }
            n -= copies;
            if (copies == 0) {
                throw "ArgumentNotFoundException";
            }
            while (arraySize - n >= 1000) {
                decrease();
            }
        }
        else {
            remove(getIndex(arg));
        }
    };

    void remove(unsigned int pos) { //remove element from specific position
        if (pos >= 0 && pos < n) {
            for (int i = pos; i < n - 1; i++) {
                array[i] = array[i + 1];
            }
            n--;
            if (arraySize - n >= 1000) {
                decrease();
            }
        }
        else {
            throw "IndexOutOfBoundException";
        }
    };

    void removeCopy(T &arg) { //leaves only one instance of an object and remove all other
        int copies = -1;
        for (int index = 0; index < n; index++) {
            if (array[index] == &arg) {
                copies++;
            }
            else if (copies > 0) {
                array[index - copies] = array[index];
            }
        }
        n -= copies;
        if (copies == -1) {
            n--;
            throw "ArgumentNotFoundException";
        }
        while (arraySize - n >= 1000) {
            decrease();
        }
    };

    void repair() { //leaves only single instance of each object
        for (int i = 0; i < n; i++) {
            removeCopy(*array[i]);
        }
    };

    void clear() { //remove all object from list
        for (int i = 0; i < n; i++) {
            array[i] = NULL;
        }
        n = 0;
    };

    T* get(unsigned int pos) { //return object on selected position
        if (pos >= 0 && pos < n) {
            return array[pos];
        }
        else {
            throw "IndexOutOfBoundException";
        }
    };

    unsigned int getIndex(T &arg) { //return position of selected object
        unsigned int index = 0;
        while (&arg != array[index] && index < n) {
            index++;
        }
        if (index == n) {
            throw "ArgumentNotFoundException";
        }
        return index;
    };

    ArrayList getSubList(unsigned int first, unsigned int last, bool deepCopy) { //return new list contains 'deep copy'/'copy reference' of all elements from (include) first to (include) last. If deepCopy=true function return deep copy, otherwise return copy of reference.
        if (first < last&&first >= 0 && last < n) {
            ArrayList<T> ret;
            for (unsigned int i = first; i <= last; i++) {
                if (deepCopy) {
                    ret.add(*new T(*array[i]));
                }
                else {
                    ret.add(*array[i]);
                }
            }
            return ret;
        }
        throw "IndexOutOfBoundException";
    };

    unsigned int size() { //return size of list
        return n;
    };

    bool isEmpty() {
        return n == 0;
    };

    T *begin() {
        return &*array[0];
    }
    T  *end() {
        return &*array[n];
    }

private:
    unsigned int arraySize; //actual size of array
    unsigned int n; //number of elements in array
    T** array;

    void increase() { //increase size of array about 1000
        if (arraySize + 1000 <= LONG_MAX) {
            T** newArray = new T*[arraySize + 1000];
            for (unsigned int i = 0; i < arraySize; i++) {
                newArray[i] = array[i];
            }
            delete[] array;
            array = newArray;
            arraySize += 1000;
        }
        else {
            throw "ArraySizeOutOfBoundException";
        }
    };

    void decrease() { //decrease size of array about 1000
        if (arraySize - 1000 > 0) {
            arraySize -= 1000;
            T** newArray = new T*[arraySize];
            for (unsigned int i = 0; i < arraySize; i++) {
                newArray[i] = array[i];
            }
            delete[] array;
            array = newArray;
        }
        else {
            throw "ArraySizeOutOfBoundException";
        }
    };
};

1 个答案:

答案 0 :(得分:1)

您发布的一些答案给出了很好的解释。 beginend将迭代器返回到一个容器中,begin引用第一个元素,end引用一个项目超过最后一个元素。至于名字,它们似乎很直观。我相信这个迭代器设计被选为指针的抽象,它的运行时成本最低。

我确定您在链接的答案中看到过此链接,但您应该参考range-based for-loops上的此页面。

在任何情况下,您似乎都对数组的元素与指向元素的迭代器感到困惑。用:

T **begin() {
    return &array[0];
}
T  **end() {
    return &array[n];
}

您的程序将使用ranged-for。您的元素类型为T*,而非T