我创建了一个名为 cosmic_ray_events 的二维向量。它有 1234487 行和 9 列。我想从所有行中找出每一列的最大值。每当我尝试运行我的代码时,我都会遇到分段错误,我知道为什么。我还通过从 dat 文件中加载值来创建 cosmic_ray_events 向量。任何建议表示赞赏。
vector<vector<double> > cosmic_ray_events(total_cosmic_ray_events, vector<double>(9,0));
ifstream cosmic_ray_data("events_comp-h4a_10.00-10000.00PeV_zen37.00.dat", ios::in);
while(cosmic_ray_data.good())
{
for(int i = 0; i < 1233487; i++) //1233487 is the number of rows in the dat file
{
for(int j = 0; j < cosmic_columns; j++)
{
cosmic_ray_data >> cosmic_ray_events[i][j]; //reading in data for 2-D vector
}
}
}
double max[9];
std::vector<double> find_max;
for(int i = 0; i < 1234487; i++)
{
for(int j = 0; j < 9; j++)
{
find_max.push_back(cosmic_ray_events[i][j]);
max[j] = *max_element(find_max.begin(), find_max.end());
find_max.clear();
}
}