Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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++ C++;-将数字表从文件读入2D数组(仅存储最后一行)_C++_Arrays_Multidimensional Array - Fatal编程技术网

C++ C++;-将数字表从文件读入2D数组(仅存储最后一行)

C++ C++;-将数字表从文件读入2D数组(仅存储最后一行),c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,从如下所示的输入文件开始: 2 3 2 3 4 4 3 2 我试图把这个数据读入C++中的一个2D数组(第一行指定行/列数)。 我的代码当前看起来像: #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; fin.open ("dataset.in"); // a matrix int a_numrows; int a_numc

从如下所示的输入文件开始:

2 3
2 3 4
4 3 2
我试图把这个数据读入C++中的一个2D数组(第一行指定行/列数)。 我的代码当前看起来像:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream fin;
  fin.open ("dataset.in");

  // a matrix
  int a_numrows;
  int a_numcols;
  int a[a_numrows][a_numcols];

  fin >> a_numrows >> a_numcols;
  cout << a_numrows << " " << a_numcols << endl;
  for (int i = 0; i<a_numrows; i++)
  {
    for (int j = 0; j<a_numcols; j++)
    {
      fin >> a[i][j];
    }
  }
  cout << a[0][0] << endl;

  fin.close();
  return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
流鳍;
fin.open(“数据集.in”);
//矩阵
国际货币基金组织;
国际货币基金组织;
int a[a_numrows][a_numcols];
fin>>a_numrows>>a_numcols;

cout您必须排列这些行:

 int a[a_numrows][a_numcols];

 fin >> a_numrows >> a_numcols;

我想这是一个疏忽的错误


也就是说,有更安全/更好的方法来声明/使用2D数组。下面是一个可能的示例:

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
  std::ifstream fin("dataset.in");

  size_t n_rows, n_cols;
  fin >> n_rows >> n_cols;

  using T = int;
  std::vector<T> array(n_rows * n_cols);
  array.assign(std::istream_iterator<T>(fin), std::istream_iterator<T>());

  fin.close();

  //-----

  for (size_t i = 0; i < n_rows; i++)
  {
    for (size_t j = 0; j < n_cols; j++)
    {
      std::cout << array[i * n_cols + j] << "\t";
    }
    std::cout << endl;
  }

  return 0;
}
记住在进行数值计算时,通常最好将所有数字存储到一个连续内存块中(就像在
std::vector
中所做的那样)。在这种情况下,编译器更容易对代码进行矢量化

要访问组件,请使用:

  • [i*n\u cols+j]
    :row-major(C-style)->给定的示例

    按此顺序循环更有效:
    fori{forj…}

  • [j*n_rows+i]
    :列主键(Fortran样式)->与&

    对于j{for i…}按此顺序循环更有效


您必须排列这些行:

 int a[a_numrows][a_numcols];

 fin >> a_numrows >> a_numcols;

我想这是一个疏忽的错误


也就是说,有更安全/更好的方法来声明/使用二维数组。以下是一个可能的例子:

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
  std::ifstream fin("dataset.in");

  size_t n_rows, n_cols;
  fin >> n_rows >> n_cols;

  using T = int;
  std::vector<T> array(n_rows * n_cols);
  array.assign(std::istream_iterator<T>(fin), std::istream_iterator<T>());

  fin.close();

  //-----

  for (size_t i = 0; i < n_rows; i++)
  {
    for (size_t j = 0; j < n_cols; j++)
    {
      std::cout << array[i * n_cols + j] << "\t";
    }
    std::cout << endl;
  }

  return 0;
}
记住在进行数值计算时,通常最好将所有数字存储到连续内存块中(就像在
std::vector
中所做的那样)。在这种情况下,编译器更容易对代码进行矢量化

要访问组件,请使用:

  • [i*n\u cols+j]
    :row-major(C-style)->给定的示例

    按此顺序循环更有效:
    fori{forj…}

  • [j*n_rows+i]
    :列主键(Fortran样式)->与&

    对于j{for i…}按此顺序循环更有效


在C++中声明数组,必须在编译时知道大小。也就是说,不能将a_numrows和a_numcols作为数组维度传递,因为它们是运行时值。对于这种方法,我将使用std::vector:

vector<vector<int>> a;
//... read a_numrows and a_numcols
a.resize(a_numrows); //resize creates #a_numrows empty columns
for(int i = 0; i < a_numrows; ++i)
{
    for(int j = 0; j < a_numcols; ++j)
    {
        int value; fin >> value;
        a[i].push_back(value); //access the ith row and add a new column with value inside
    }
}
向量a;
//... 读一本书和一本书
a、 调整大小(a_numrows)//调整大小将创建#a#个空列
对于(int i=0;i>值;
a[i].push_back(value);//访问第i行并添加一个包含值的新列
}
}

<代码> >在C++中声明数组,必须在编译时知道大小。也就是说,不能将a_numrows和a_numcols作为数组维度传递,因为它们是运行时值。对于这种方法,我将使用std::vector:

vector<vector<int>> a;
//... read a_numrows and a_numcols
a.resize(a_numrows); //resize creates #a_numrows empty columns
for(int i = 0; i < a_numrows; ++i)
{
    for(int j = 0; j < a_numcols; ++j)
    {
        int value; fin >> value;
        a[i].push_back(value); //access the ith row and add a new column with value inside
    }
}
向量a;
//... 读一本书和一本书
a、 调整大小(a_numrows)//调整大小将创建#a#个空列
对于(int i=0;i>值;
a[i].push_back(value);//访问第i行并添加一个包含值的新列
}
}

Wow。真不敢相信我这么做了。只是需要其他人来看看,因为我在过去的10-15分钟里一直盯着这些台词,非常困惑。只要时间允许,我会尽快接受这个答案。谢谢@我用一个例子更新了我的答案。真不敢相信我这么做了。只是需要其他人来看看,因为我在过去的10-15分钟里一直盯着这些台词,非常困惑。只要时间允许,我会尽快接受这个答案。谢谢@我用一个例子更新了我的答案