将std :: vector扩展为具有NULL对象的动态数组

时间:2012-07-06 15:30:33

标签: c++ stdvector derived-class

正如标题所说,我尝试以这种方式扩展std :: vector类,如果我擦除一个元素,该位置的值不是擦除但实际设置为NULL(提供一个间隙)。

template<typename T>
class FVector : public std::vector<T> {
    typedef std::vector<T> Base;

protected:
    /** The number of elements in the vector */
    size_t elementCount;

    /**
     * The index of the last element. This field is actually vector's length. 
     * For example when you have a vector like this ["a","b","c"] then the 
     * elementCount would be 3 and lastIndex would be 2 (because indexes are 
     * zero-based). But if you erased two first elements, 
     * leaving [null, null, "c" ] then elementCount=1 (because there is only 
     * one element in the vector!) however lastIndex would still remain 2. 
     * After you erase "c" lastIndex would be set to -1 (so it's enough to 
     * add 1 to lastIndex to determine vector's length.
     */
    int lastIndex;

private:
    /**
     * Returns the index of the last not-null element in the vector, 
     * starting from position position downwards.
     *
     * @param position the position from which counting is to be begun.
     * @return last element before (or on) index <code>position</code>
     */
    int FindLastIndex(int position) {
        int nLastItem = position;

        if (position < 0) {
            return -1;
        }

        for (; nLastItem >= 0; nLastItem--) {
            if (Base::operator[](nLastItem) != NULL) {
                break;
            }
        }

        return (nLastItem);
    }

public:

    FVector(const T & value = T())
    : elementCount(0), lastIndex(-1) {
    }

    FVector(int initialCapacity, const T & value = T())
    : elementCount(0), lastIndex(-1),
      std::vector<T>(initialCapacity, value) {
    }

    size_t Capacity() const {
        return Base::size();
    }

    size_t Size() const {
        return elementCount;
    }

    int LastIndex() const {
        return lastIndex;
    }

    void AddElement(const T& obj) {
        Base::push_back(obj);

        elementCount++;
        lastIndex++;
    }

    T & ElementAt(int index) {
        if (index > lastIndex) {
            // error
        }

        return Base::at(index);
    }
    void EraseElementAt(int index) throw() {
        if (index > lastIndex) {
            std::stringstream ss;
            ss << index << " > " << lastIndex;
            throw ArrayIndexOutOfBoundsException(ss.str());
        }

        if (Base::operator[](index) != NULL) {
            elementCount--;
            T v = Base::at(index);
            delete v;
            Base::at(index) = NULL;

            if (index == lastIndex) {
                lastIndex = FindLastIndex(lastIndex - 1);
            }
        }
    }
};

它没有像我期望的那样工作。当我在一个元素上调用erase()方法时该元素 未设置为NULL。

例如:

class Int {
    int i;
public:
     Int(int v): i(v) { };
     ~Int() { }
};

//... 

FVector<Int *> * v = new FVector<Int *>();

v->AddElement(new Int(1));
v->AddElement(new Int(3));
v->AddElement(new Int(5));
v->EraseElementAt(0);
v->EraseElementAt(2);

// ...

delete v;

将导致

[null,3]

但我希望它是

[null,3,null]

嗯,我不知道这是否有可能实现。我想采用std :: vector类,这是一个动态数组(那么我为什么要编写自己的数组类)给我提供实现这些东西所需的所有基础知识。

任何人都可以对此有所了解,我想我在这里有一些实施问题。

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

在你的EraseElement中你有这个:

        if (index == lastIndex) {
            lastIndex = FindLastIndex(lastIndex - 1);
        }

如果碰巧擦除了向量中的最后一个元素(你做了),它将缩短向量(减去lastIndex)。看起来你希望你的矢量不要这样做 - 而是你想让矢量为空而不是缩短。也许只是把它拿出来。