【问题标题】:2D array size problem loading from csv file从 csv 文件加载二维数组大小问题
【发布时间】:2020-12-29 23:05:56
【问题描述】:
int main()
{

    auto base_dir = fs::current_path();
    auto dataset_1 = data_names_1;
    auto dataset_name = base_dir / "NAimg_20101026_145727.csv";

    //double data[10][3];
    std::ifstream file(dataset_name);

    matrix<DataType> data_length;
    file >> data_length;
    
    auto pre_num_row = data_length.nc();
    static const int num_row = 73;//data_length.nr();
    static const int num_column = 74496; //data_length.nc();

    std::cout << num_row << "\n";
    std::cout << num_column << "\n";

    double data[73][74496];

    for (int row = 0; row < 73; ++row)
    {
        std::string line;
        std::getline(file, line);
        if (!file.good())
            break;

        std::stringstream iss(line);

        for (int col = 0; col < 74496; ++col)
        {
            std::string val;
            std::getline(iss, val, ',');
            if (!iss.good())
                break;

            std::stringstream convertor(val);
            convertor >> data[row][col];
        }
    }

    std::cout << data[1][1] << "\n";
  
}

您好,我的名字是 Q。我想读取一个 csv 文件(这里文件的名称是 NAimg_20101026_145727.csv)并将数据加载到数据数组中。文件的行列大小分别为 73 和 74496。因此,我需要双 data[73][74496] 数组。它会出错。当我制作一个小数组时,例如 double data[10][20],没有错误。你知道是什么问题吗?

我还有一个问题。我想从 csv 文件中读取行和列的大小并自动确定双数据 [行] [列] 的大小。因此,我尝试使用 data_length.nc();决定双倍数据大小的行和列。您知道如何从 csv 文件中确定双数据 [行] [列] 的大小吗?谢谢

【问题讨论】:

  • 您的代码需要#includes 和namespace fs = std::filesystem; 等才能工作。不要为您的 minimal reproducible example 删除必要的代码,以使其具有可重复性。
  • double data[73][74496]; 将是 43,505,664 字节,超过您的堆栈大小至少 1 或 5 倍。(可能是 8 倍,如果在 Windows 上为 43 倍)使用 std::vector&lt;std::vector&lt;double&gt;&gt; 或动态分配(std::vector 是首选)

标签: c++ arrays csv


【解决方案1】:

您似乎希望基于 data_length.nr()data_length.nc() 创建二维数组,这可能仅在运行时才知道,因此您需要动态分配。一种简单的方法是 #include &lt;vector&gt; 并像这样创建它:

#include <vector>

// ...

    // create a 2D vector from nr() and nc()
    std::vector<std::vector<double>> data(data_length.nr(), 
                                          std::vector<double>(data_length.nc()));
    
    // fill the 2D vector with data from the file using range-based for loops:
    // Assuming csv lines formatted like: 1,2,3,4,5<newline>6,7,8,9,10<newline> ...
    for(auto& row : data) {
        for(auto& col : row) {
            file >> col;
            file.ignore(); // ignore one char (commas and newlines)
        }
    }

    if(file) {
        // successfully read the data from the file
        std::cout << data[1][1] << '\n';

    } else {
        std::cout << "failed extracting data from file\n";
    }

【讨论】:

  • 泰德,非常感谢。我遵循了您的代码,但它不起作用。我在另一个问题上发布了我的代码。谢谢!
  • @Kyuwan 为什么要打开一个新问题?如果您需要澄清任何事情,请编辑此问题。 ...而且,我很确定我的代码可以正常工作,所以请解释一下 如何 它不起作用。编辑:好的,我看到新问题使用了我的代码 :-)
  • @Kyuwan Btw,matrix 是您自己可以更改的课程模板吗?
  • 不知道为什么无法从csv文件中读取数据。
  • @Kyuwan 我看到了对问题的一些建议编辑,但这些会从根本上改变问题,所以我不想批准这些。请提供数据文件的示例,我会在之后更新我的答案。
【解决方案2】:

数据数组放在栈上,大小有限制。我的版本在堆上分配数组。但我想你可以添加 c++ 运算符。

#include <iostream>

#include <stdlib.h>

typedef struct DoubleArray2D{
    int width = 0;
    int height = 0;
    double *data = NULL;
    void allocate(int rows, int columns){
        width = rows;
        height = columns;
        data = (double *)malloc(sizeof(double)*width*height);
    }
    void deallocate(){
        width = 0;
        height = 0;
        free(data);
    }

    double get(int row,int column){
        return data[column*height+row];
    }

    void set(int row, int column,double value){
        data[column*height+row] = value;
    }
}DoubleArray2D;
int main(){
    DoubleArray2D data;
    data.allocate(73,74496);

    data.set(1,1,20.0f);

    std::cout << data.get(1,1) << std::endl;
    data.deallocate();
}

【讨论】:

  • 这是 C 语言,混合了一点 C++。根本不需要使用 mallocs 或 frees。甚至不需要new[]s 和delete[]s。此外,typedef struct 中的 typedef 是多余的。在 C++ 中自动为 typedefined。
  • 嗨,NoNae,感谢您的回复。这对我来说是非常有帮助的。我成功制作了 DoubleArray2D 数据。但是,我不知道如何将数据从 csv 文件加载到 DoubleArray2D 数据中。
  • 如何将data.allocate(73,74496)与csv文件连接起来?
  • 嗨,NoNae,你能检查一下更新的代码吗?有问题。
  • 这里,n_row = 73,n_col = 74496,for 循环不起作用。你知道原因吗?
猜你喜欢
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2019-12-08
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
相关资源
最近更新 更多