我制作的矢量不能在c ++中制作一个二维矢量

时间:2016-10-13 03:09:52

标签: c++ vector

我被指示为我的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;
    }

1 个答案:

答案 0 :(得分:1)

“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"