[英]Why can't I push a vector of integers to a 2-D vector of integers in C++?
[英]Vector I made can't make in c++ a 2-d 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;
}
“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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.