[英]Problem with 2D vector in c++ when printing
class Vertex {
public:
int dist, num;
vector<Vertex*> edges;
Vertex() {}
Vertex(int num) : num(num) {}
};
class K {
public:
void updateMatrix(vector<vector<int>>& matrix) {
vector<vector<Vertex>> vec;
for (vector<int> in : matrix) {
vector<Vertex> neu;
vec.push_back(neu);
for (int i : in) {
Vertex k(i);
cout << k.num;
neu.push_back(k);
}
}
for (vector<Vertex> in : vec) {
for (Vertex i : in) cout << i.num << " ";
cout << endl;
}
}
};
When I call the function with a valid 2D vector, when I try to print vec
the output is just 3 blank lines.当我用有效的 2D 向量调用 function 时,当我尝试打印
vec
时,output 只有 3 个空行。 How can I fix this?我怎样才能解决这个问题?
vec.push_back(neu);
puts a copy of the (empty) variable neu
into vec
.将(空)变量
neu
的副本放入vec
。 Changes to neu
after this don't affect the copy.在此之后对
neu
的更改不会影响副本。
You should move that to after the inner loop.您应该将其移至内部循环之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.