[英]Convert int[n][n] to vector<vector<int>>
We can easily convert an array to a vector with the following:我们可以使用以下方法轻松地将数组转换为向量:
int a[n];
vector<int> b = vector(a, a + n);
I want to work with matrix, what if I want to convert:我想使用矩阵,如果我想转换怎么办:
int a[n][n];
to至
vector<vector<int>> b = ... // from a
with b.size() = n and b[0...n-1].size() = n? b.size() = n 和 b[0...n-1].size() = n?
Alternatively I am okay with a solution converting或者,我可以接受转换的解决方案
std::vector< std::array<int, n> > a;
a.reserve(n);
vector<vector<int>> b = ... // from a
I doubt that vector of vector is the best choice of the container for your use case, but if it's out of your control something like this should work:我怀疑向量的向量是您用例的容器的最佳选择,但如果它超出您的控制范围,这样的事情应该可以工作:
int a[n][n];
std::vector<std::vector<int>> v;
v.reserve(n);
for (int *arr : a) {
v.emplace_back(arr, arr + n);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.