0

I have a 2D vector that I'm trying to populate with coordinates. 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.

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; 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. This happens on any value of cr.

Can anyone shed some light on this problem? Thanks for any help!

2

1 Answer 1

1

As @Erik pointed out, you have a typo. 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. 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.

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);
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.