[英]Convert vector<string> to 2D vector<int> without vector<vector<int>>?
I've got a 2D vector of strings: 我有一个二维的字符串向量:
AAB
BAB
BAA
And I want to build a 2D vector of integers based on what values it finds at each [i][j]
, which I have already done this using a std::vector<std::vector<int>>
to produce: 我想根据在每个
[i][j]
找到的值来构建一个整数的二维向量,我已经使用std::vector<std::vector<int>>
进行了以下操作以产生:
110
010
011
But this is proving problematic for a later use of the 2D vector of integers as I need to use it in a BFS but as a std::vector<int>
. 但这对于后来使用整数的二维向量是有问题的,因为我需要在BFS中将其用作
std::vector<int>
。 So is there a way to keep the 2D schema without using std::vector<std::vector<int>>
? 那么有没有办法在不使用
std::vector<std::vector<int>>
情况下保留2D模式?
Yes, instead of having two vectors and indexing as v[i][j]
, you can use one vector and index as v[t]
. 是的,您可以使用一个向量并将索引作为
v[t]
来代替具有两个向量并以v[i][j]
索引。 The size of your vector will be w * h
instead of having vectors of size w
(width) and size h
(height). 向量的大小将为
w * h
而不是大小为w
(宽度)和大小为h
(高度)的向量。
You can use the single value t
as an index. 您可以将单个值
t
用作索引。 Here t = i + j * w
, and w
is the row width, in your case w=3
. 这里
t = i + j * w
, w
是行宽度,在您的情况下w=3
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.