我制作的向量无法在 c++ 中制作 2-D 向量

Vector I made can't make in c++ a 2-d vector

本文关键字:向量 c++ 2-D      更新时间:2023-10-16

我被指示为我的CS类制作一个向量。当我想要一个一维的矢量时,我的矢量工作得很好,但是如果我想要

MyVector<MyVector<Some_Class>> mv;

只要上面的代码中只存储了一个向量,它就可以工作。

// this is fine
mv.resize(1);
Some_Class sc_var;
mv[0].push_back(sc_var);
cout << mv[0].size() << endl;

但是如果我这样做

// this is where the bug is found
mv.resize(2);
Some_class sc_var2;
mv[1].push_back(sc_var2);
cout << mv[0].size() << ' ' << mv[1].size() << endl;
// it will give the output '0 1' when it should give '1 1'

我追踪到我的代码的这一部分的错误

T *temp_array = new T[cur_size];
for (int i = 0; i < cur_size; i++) {
    temp_array[i] = safe_array[i];
}

这部分代码在resize方法中。在调试时,我发现temp_array[I]不会被设置为与safe_array[I]相同的值。这是一个问题,因为我使用temp_array来临时存储来自safe_array的所有内容,因此safe_array可以调整大小以容纳更多或更少的元素。因此,由于temp_array[i]没有被设置为safe_array[i],当temp_array的值被反馈到safe_array时,temp_array[i]仍然被设置为新的MyVector[cur_size]。

// this is how I got the type of T
cout << typeid(T).name() << endl;
// class MyVector<class Some_Class>

这个值被反馈到safe_array中。为什么会发生这种情况,我该如何解决?

这里有一个更好的例子来说明我想说的

Class Cell{
    char data;
    Cell *north = nullptr;
    Cell *east = nullptr;
    Cell *south = nullptr;
    Cell *west = nullptr;
}
MyVector<MyVector<Cell>> mv;
mv.resize(1);
// the below code will work find
mv[0].push_back(9);
cout << mv[0].size() << 'n';
// will print out 1
// below is code that won't work
mv.resize(2);
mv[1].push_back(8);
cout << mv[0].size() << ' ' << mv[1].size() << 'n';
// will print out '0 1', but should print out '1 1'

下面是我的调整大小方法

// resizes the array to any size the programmer wants
    void resize(int new_cur_size) {
        int container_size = new_cur_size;
        if (container_size < 10) {
            container_size = 10;
        }
        if (new_cur_size <= 0) {
            SafeArrayException ex;
            throw ex;
            return;
        }
        // creates a new dynamic array the same size and type
        // as the underlying array so the underlying array can
        // be copied, resized, and refilled
        T *temp_array = new T[cur_size];
        // copies everything from underlying array to temp array
        for (int i = 0; i < cur_size; i++) {
            // this part is the problem
            // temp_array[i] will not be set to
            // safe_array[i] (when mv.resize() is called
            // not when something like mv[0].resize() is executed
            temp_array[i] = safe_array[i];
        }
        // resizes the array
        delete[] safe_array;
        safe_array = new T[container_size];
        // fills the underlying array back up with its old elements
        // in the same position they where at before resizing
        for (int i = 0; i < new_cur_size; i++) {
            if (cur_size > 0 && i <= cur_size - 1) {
                safe_array[i] = temp_array[i];
            }
        }
        // lets template know that the array has a new compacity
        // and frees up memory used by temp array
        container_cur_size = container_size;
        cur_size = new_cur_size;
        delete[] temp_array;
    }

"0 1"应该是预期的输出。mv[0]处的向量是一个空向量,因为你没有给它添加(回推)一个元素。如果你对这个向量执行push_back操作,它的大小也会变成1:

mv.resize(2);
Some_class sc_var2;
mv[0].push_back(sc_var2); //mv[0] wont be empty once this line has run
mv[1].push_back(sc_var2);
cout << mv[0].size() << ' ' << mv[1].size() << endl; //"1 1"