在2d向量的特定位置插入 [英] Insert at specific location of a 2d vector
问题描述
我有一个2d向量,代表一个2d网格;因此 grid [0] [2]
。我需要插入 - 这里不是正确的词。在特定位置的向量说 grid [3] [2]
将会有 grid [0] [0]
但是当需要 insert 到 grid [3] 2]
除了 grid [0] [0]
之前可能没有任何东西,并且需要在之间的空格。有任何方法吗?
I have a 2d vector which represents a 2d grid; so grid[0][2]
for example. I am needing to 'insert' -might not be the right word here. a vector at a specific location say grid[3][2]
there will definitely be a grid[0][0]
but when im needing to insert into grid[3][2]
there may be nothing before it other than grid[0][0]
and there needs to be the space in between for later on. Is there any way to do this?
感谢您的帮助。
ps:I应该注意向量的大小是未知的(它们将随时间增长)。
ps: I should note that the size of the vectors are not known (they will grow over time)
推荐答案
不确定我是否正确理解您的要求,但:
I'm not sure if I understand your requirements correctly, but:
std::vector<std::vector<int>> grid(4); // 4 rows
grid[3].resize(3) // resize 4th row
grid[3][2] = 42;
您的2D网格会像这样看起来:
Your 2D grid would then "look" like that:
|
-
|
- <--- 3 empty rows
|
--------------
| 0 | 0 | 42 |
--------------
稍后调整行大小。注意,有0行,但还没有[0] [0]元素,你必须自己添加。
You can freely resize the rows later on. Note that there is row 0, but no [0][0] element just yet, you have to add it yourself.
这篇关于在2d向量的特定位置插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!