[英]Attempting to push_back() a struct results in incorrect information in 2D vector
I have a 2D vector that I'm trying to populate with coordinates. 我正在尝试使用坐标填充2D向量。 I've defined a struct for the coordinates for my own sake, but for some reason the push_back() function is not pushing the correct y-coordinate onto the vector, instead only pushing the first one.
我已经为自己定义了一个结构体,但是出于某种原因,push_back()函数没有将正确的y坐标推入向量,而是仅推了第一个。
Here is the code in question: the rest of the code is not too important to this snippet. 这是有问题的代码:其余代码对该代码段不太重要。
struct coor2d {
float x;
float y;
};
// Inside a function
int row = 5;
int col = 5;
float r_wide = 1.0/float(row);
float r_high = 1.0/float(col);
vector<vector<coor2d> > grid;
vector<coor2d> column;
for(int cr = 0; cr < row; cr++) {
for(int cc = 0; cr < col; cc++) {
coor2d temp;
temp.x = (float(cc) * r_wide) + (r_wide/2.0);
temp.y = ((float(cr) * r_high) + (r_high/2.0) * -1.0);
// Here the temp.y value is correct
column.push_back(temp);
// Here the temp.y value is incorrect
}
grid.push_back(column);
}
The rest of the code depends upon this working properly. 其余代码取决于此是否正常工作。 I assume I'm losing precision or have called something incorrectly here.
我认为我正在失去精度或在这里打错了什么。 I know I could make a constructor for coor2d, but I don't think that would solve this problem;
我知道我可以为coor2d构造一个构造函数,但是我认为这不能解决这个问题。 however, I could be wrong.
但是,我可能是错的。
An example of the problem manifesting: 问题体现的一个示例:
Once through the first iteration of the for(cr < row) loop, the inner for(cc < col) loop outputs the proper x-coordinates, but after the column.push_back(temp) is complete, the y-coordinate acts as though cr is still 0.0f instead of 1.0f, outputting -0.1f into the vector instead of the proper -0.3f. 一旦通过for(cr <row)循环的第一次迭代,内部的for(cc <col)循环将输出正确的x坐标,但是在column.push_back(temp)完成后,y坐标的作用就像cr仍然是0.0f而不是1.0f,将-0.1f输出到向量中,而不是正确的-0.3f。 This happens on any value of cr.
任何cr值都会发生这种情况。
Can anyone shed some light on this problem? 任何人都可以阐明这个问题吗? Thanks for any help!
谢谢你的帮助!
As @Erik pointed out, you have a typo. 正如@Erik指出的那样,您有错字。 This:
这个:
for(int cc = 0; cr < col; cc++) {
should be this: 应该是这样的:
for(int cc = 0; cc < col; cc++) {
also, I think you probably want to "reset" your column
vector on each pass of the outer for-loop. 另外,我认为您可能想在外部for循环的每一遍上“重置”
column
向量。 I think the easy way is to just move it: 我认为最简单的方法就是移动它:
vector<vector<coor2d> > grid;
for(int cr = 0; cr < row; cr++) {
vector<coor2d> column; // move the column vector to here
for(int cc = 0; cr < col; cc++) {
If you don't do this, the column
vector just accumulates all the values you push onto it. 如果您不这样做,则
column
向量只会累加您推送到其上的所有值。
With those changes, I get what I consider to be "sane" output from this test program: 经过这些更改,我从该测试程序中得到了我认为是“合理”的输出:
#include <iostream>
#include <vector>
struct coor2d {
float x;
float y;
};
int main(){
// Inside a function
int row = 5;
int col = 5;
float r_wide = 1.0/float(row);
float r_high = 1.0/float(col);
for(int cr = 0; cr < row; cr++) {
std::vector<coor2d> column;
for(int cc = 0; cc < col; cc++) {
coor2d temp;
temp.x = (float(cc) * r_wide) + (r_wide/2.0);
temp.y = ((float(cr) * r_high) + (r_high/2.0) * -1.0);
// Here the temp.y value is correct
column.push_back(temp);
// Here the temp.y value is incorrect
std::cout << "temp.x: " << temp.x << " temp.y: " << temp.y << std::endl;
std::cout << "vec.x: " << column[cc].x << " vec.y: " << column[cc].y << std::endl;
}
grid.push_back(column);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.