我制作的向量无法在 c++ 中制作 2-D 向量
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"
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么