c++ - Creating 2-D vector with values from another 2-D vector -
i have 2-d vector called cosmic_ray_events has size equal 898281 , created 2-d vector called high-energy_cosmic_rays. put in check in cosmic_ray_elements filters out values less 100 , located @ cosmic_ray_events[i][3]. if value greater 100 transfer elements associated cosmic_ray_event[i][j] 2-d vector or resize current 2-d vector contains vectors had cosmic_ray_events[i][3] greater 100. segmentation fault when try set cosmic_ray_events[i][j] equal 2-d vector. not sure how transfer elements 1 2-d vector without getting segmentation fault.
vector<vector<double> > high_energy_cosmic_rays(he_cr, vector<double>(9,0)); for(int = 0; < cosmic_ray_events.size(); i++) { if(cosmic_ray_events[i][3] >= 100.) { for(int j = 0; j < 9; j++) { high_energy_cosmic_rays[i][j] = cosmic_ray_events[i][j]; } } }
your presizing result vector, isn't possible because have no idea goes in yet. "safe" pre-size use same size original vector, doesn't make sense when you're trying filter them down inner vectors have slot3 content >= 100.
what need start empty vector, , use insertion or push_back add vectors in original vector of vectors target have slot3 >= 100. 1 way using
- an empty vector of vectors.
high_energy_cosmic_raysbelow. - a copy enumeration conditionally copies when expression true item in iteration sequence.
std::copy_ifcan that. - an output inserter performs pushback operation. using
std::back_inserterwrapped around target vector of vectors object output iteratorstd::copy_ifthat.
the results this:
std::vector<std::vector<double>> high_energy_cosmic_rays; std::copy_if(cosmic_ray_events.cbegin(), cosmic_ray_events.cend(), std::back_inserter(high_energy_cosmic_rays), [](std::vector<double> const& v) { return v[3] >= 100; }); unrelated, take care when comparing floating point values.
Comments
Post a Comment