I created a fixed array e_pt[10]
initialised with zeros. I then filled in some values into the array e_pt
. I would then like to find the index of the largest element in the array. The code is shown below.
double e_pt[10] = {};
for (size_t lep_i=0; lep_i<lep_n; lep_i++) // loop over leptons
{
if (lep_type->at(lep_i) == 11) // record kinematic info of electrons into array
{
e_pt[lep_i] = lep_pt->at(lep_i);
}
} // end of loop over leptons
int e_index = std::distance(e_pt.begin(), std::max_element(e_pt.begin(), e_pt.end()));
However when I try to compile this, the following error occurs:
member reference base type 'double [10]' is not a structure or union
It is refering to the action e_pt.begin() being invalid.
What is wrong here?
std::max_element(e_pt.begin(), e_pt.end())
Native array doesn't have methods like that.
It should be
std::max_element(std::begin(e_pt), std::end(e_pt));
This error relates to use of . operator on array. Your loop can be simpler, if lep_n
is size of lep_pt
and lep_pt
is container compatible with C++ ranges.
size_t lep_i = 0;
for (auto pt : lep_pt)
if (lep_type->at(lep_i) == 11)
e_pt [lep_i++] = pt;
Or vice versa relative to lep_type
.