C++ C+中的计数错误+;

C++ C+中的计数错误+;,c++,matrix,C++,Matrix,我正在写一个程序来输出矩阵的转置。当我在正方形或列多于行的矩阵上使用它时,该程序可以工作,但当我试图在行多于列的矩阵上使用它时,会给出错误的输出 程序的jist是将数字读入向量,然后将该向量推送到2D向量数据。这部分代码运行良好(据我所知),我认为错误在这些循环中 if (data[0].size() < data.size()) // number of columns < number of rows BREAKS { // test52 gets here for(size_t

我正在写一个程序来输出矩阵的转置。当我在正方形或列多于行的矩阵上使用它时,该程序可以工作,但当我试图在行多于列的矩阵上使用它时,会给出错误的输出

程序的jist是将数字读入向量,然后将该向量推送到2D向量
数据
。这部分代码运行良好(据我所知),我认为错误在这些循环中

if (data[0].size() < data.size()) // number of columns < number of rows BREAKS
{ // test52 gets here
for(size_t i=0; i<data[0].size(); ++i) // loops over the number of rows
{
  for(size_t j=0; j<data.size(); ++j) // loops over the number of columns (the number of entries in each row)
  {
    cout << data[j][i] << "\t";
  }
  cout << endl;
}
输出是
1 3 5 7 9
,因此缺少第二列

编译和运行时,我没有收到任何错误。我已经盯着代码看了一个多小时了,我一辈子都搞不清楚到底出了什么问题

任何帮助都将不胜感激

编辑

对不起,各位,程序运行正常。问题是我试图让程序读取错误格式的文本文件(我忘记了逗号)

谢谢您的帮助。

替换

i<data[i].size();
i这个:


for(size\u t i=0;iRun在调试器中,逐行遍历代码,观察变量(尤其是
data[i]
data[j]
)。只要尝试一下,程序就会给出与以前相同的输出
i<data[i].size();
i<data[0].size();
for(size_t i=0; i<=data[0].size(); ++i) // loops over the number of rows
for(size_t i=0; i<data[0].size(); ++i) // loops over the number of rows