Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.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++_Max_2d Vector - Fatal编程技术网

C++ 在二维向量的每列中求最大值

C++ 在二维向量的每列中求最大值,c++,max,2d-vector,C++,Max,2d Vector,我已经创建了一个叫做宇宙射线事件的二维向量。它有1234487行和9列。我想从所有行中找出每列的最大值。每当我试图运行我的代码时,我总是会遇到一个分段错误,我知道为什么。我还通过加载dat文件中的值创建了宇宙射线事件向量。任何建议都将不胜感激 vector<vector<double> > cosmic_ray_events(total_cosmic_ray_events, vector<double>(9,0)); ifstream cosmic_ray_d

我已经创建了一个叫做宇宙射线事件的二维向量。它有1234487行和9列。我想从所有行中找出每列的最大值。每当我试图运行我的代码时,我总是会遇到一个分段错误,我知道为什么。我还通过加载dat文件中的值创建了宇宙射线事件向量。任何建议都将不胜感激

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();
    }
}
矢量宇宙射线事件(总宇宙射线事件,矢量(9,0));
ifstream宇宙射线数据(“事件comp-h4a\U 10.00-10000.00PeV\U zen37.00.dat”,ios::in);
while(宇宙射线数据.good())
{
for(int i=0;i<1233487;i++)//1233487是dat文件中的行数
{
对于(int j=0;j>宇宙射线事件[i][j];//读取二维矢量的数据
}
}
}
双倍最大值[9];
std::向量find_max;
对于(int i=0;i<1234487;i++)
{
对于(int j=0;j<9;j++)
{
找到最大推回(宇宙射线事件[i][j]);
max[j]=*max_元素(find_max.begin(),find_max.end());
查找_max.clear();
}
}

既然您使用的是
std::vector
,您可以帮自己一个忙,在每次查找时进行范围检查。这将防止segfault并返回可理解的错误消息。这样做看起来像这样:

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.at(i).at(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.at(i).at(j));
    max[j] = *max_element(find_max.begin(), find_max.end());
    find_max.clear();
  }
}

谢谢,我想最后一组循环会给我一个只包含9个元素的向量,每个元素都是每列的最大值。我应该把find_max变成一个二维向量吗?
std::vector<double> max_vals(9,-std::numeric_limits<double>::infinity());
for(int i = 0; i < 1234487; i++){
  for(int j = 0; j < 9; j++){
    max_vals.at(j) = std::max(max_vals.at(j),cosmic_ray_events.at(i).at(j));
  }
}