简体   繁体   English

C++ 复制多维向量

[英]C++ copying multidimensional vector

I'm having problems copying a multidimensional vector, I've tried many things but this is the last one:我在复制多维向量时遇到问题,我尝试了很多方法,但这是最后一个:

vector < vector < int > > a;
vector < vector < int > > b;
a.resize(10);
b.resize(10);
a[0][0] = 123;
copy( a.begin(), a.end(), back_inserter(b) );
cout << b[0][0];

I'm trying to do a recursive loop that counts all possible routes in a grid within 10 moves.我正在尝试做一个递归循环,计算网格中 10 次移动中所有可能的路线。 I'm trying to create a vector called current_path which would hold the current path for each recursion, when the current_path has 10 moves, It will copy the data from current_path to all_paths .我正在尝试创建一个名为current_path的向量,它将保存每个递归的当前路径,当current_path有 10 次移动时,它会将数据从current_path复制到all_paths

The grid goes like this:网格是这样的:

0  1  2 3
4  5  6 7
8  9  10 11
12 13 14 15

You can only move to a square you touch so from 0 you can move to 1, 4 and 5. And from 1 to 3, 4, 5, 6 etc.您只能移动到您触摸的方格,因此从 0 您可以移动到 1、4 和 5。从 1 到 3、4、5、6 等。

The main idea is to copy the current_path to the next function call (recursive) so it would hold the curren_path up to that point, doing that until it's full (10 steps).主要思想是将current_path复制到下一个函数调用(递归),以便将curren_path保持到那个点,直到它满为止(10 步)。 After it's copied from current_path to all_paths I suppose I have to delete the current_path ?current_path复制到all_paths我想我必须删除current_path

I know how to efficiently calculate all steps but I'm having trouble copying the current_path and propably and how do I add the current_path to all_paths when I'm at 10 steps?我知道如何有效地计算所有步骤,但是我在复制current_path遇到了麻烦, all_paths在我执行 10 个步骤时如何将current_path添加到all_paths

There are a few problems with your code.您的代码存在一些问题。 By the end of line 4, you have two vectors that each contain 10 empty vectors.在第 4 行的末尾,您有两个向量,每个向量包含 10 个空向量。 You could visualize it like this:你可以像这样想象它:

a = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
b = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}

Those inner vectors still do not have any elements so when you try and set a[0][0] to 123 , you're accessing an element that doesn't exist, invoking undefined behaviour.这些内部向量仍然没有任何元素,因此当您尝试将a[0][0]123 ,您正在访问一个不存在的元素,从而调用未定义的行为。

If that had worked, your use of std::copy would simply copy each of the vectors from a and pushed it to the back of b .如果这样做有效,您使用std::copy将简单地从a复制每个向量并将其推送到b的后面。 Since b already has 10 elements, it would now have 20 elements.因为b已经有 10 个元素,所以它现在有 20 个元素。

Then you try to output b[0][0] which doesn't exist just as much as a[0][0] didn't either.然后尝试输出b[0][0]不存在丝毫不亚于a[0][0]确实没有。

The solution here is to simply use the copy assignment operator defined by std::vector :这里的解决方案是简单地使用std::vector定义的复制赋值运算符:

vector<vector<int>> a = {{1, 2, 3}, {4, 5}};
vector<vector<int>> b;
b = a;

You can just do b = a;你可以只做b = a;

std::vector defines a copy assignment operator which does an elementwise copy. std::vector定义了一个复制赋值运算符,它进行元素复制。 This will invoke the copy assignment operator of the inner vector, which copies the int s.这将调用内部向量的复制赋值运算符,它复制int s。

Instead of代替

a.resize(10);
a[0][0] = 123;

You will want to do你会想做

a.resize(10);
a[0].push_back(123);

because while resize creates 10 new vectors in the outer vector, these inner vectors have length 0, so doing a[0][0] will give you an element one past the end of the first inner vector.因为虽然resize在外部向量中创建了 10 个新向量,但这些内部向量的长度为 0,因此执行a[0][0]将为您提供一个超出第一个内部向量末尾的元素。

Also, as long as you create the vectors on the stack (as you have done) you will not need to delete anything;此外,只要您在堆栈上创建向量(正如您所做的那样),您就不需要删除任何内容; they have automatic storage duration .他们有自动存储期限

Here is the fixed version of your code:这是您的代码的固定版本:

vector < vector < int > > a;
vector < vector < int > > b;
a.resize(10, vector < int >(10));
b.resize(10, vector < int >(10));
a[0][0] = 123;
b = a;
cout << b[0][0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM