Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图推回结构会导致2D vector中的信息不正确_C++_Vector - Fatal编程技术网

C++ 试图推回结构会导致2D vector中的信息不正确

C++ 试图推回结构会导致2D vector中的信息不正确,c++,vector,C++,Vector,我有一个二维向量,我想用坐标填充它。我已经为坐标定义了一个结构,但出于某种原因,push_back()函数没有将正确的y坐标推送到向量上,而是只推第一个坐标 下面是有问题的代码:其余的代码对于这个代码段来说并不太重要 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/f

我有一个二维向量,我想用坐标填充它。我已经为坐标定义了一个结构,但出于某种原因,push_back()函数没有将正确的y坐标推送到向量上,而是只推第一个坐标

下面是有问题的代码:其余的代码对于这个代码段来说并不太重要

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);
}
struct-coor2d{
浮动x;
浮动y;
};
//函数内部
int行=5;
int col=5;
浮动r_宽=1.0/浮动(行);
浮子r_高=1.0/浮子(col);
矢量网格;
向量列;
对于(int cr=0;cr
代码的其余部分取决于它是否正常工作。我想我正在失去准确度,或者在这里调用了错误的东西。我知道我可以为coor2d创建一个构造函数,但我认为这不能解决这个问题;然而,我可能错了

问题的一个例子是:

一旦通过for(cr
有人能解释一下这个问题吗?谢谢你的帮助

正如@Erik所指出的,你的拼写错误。这:

for(int cc = 0; cr < col; cc++) {
如果不这样做,
向量只会累加推到它上的所有值

随着这些变化,我从这个测试程序中得到了我认为是“明智”的输出:

#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);
  }
}
#包括
#包括
结构coor2d{
浮动x;
浮动y;
};
int main(){
//函数内部
int行=5;
int col=5;
浮动r_宽=1.0/浮动(行);
浮子r_高=1.0/浮子(col);
对于(int cr=0;crstd::无法在
中将
cr
更改为
cc
(int cc=0;cr
好的,你能为我们提供一个吗?至少一个惊喜我很笨。谢谢大家的帮助!
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++) {
#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);
  }
}