jeudi 22 février 2018

Iterator pointing to empty element of array

I am just wondering whether in an array which has elements, but is not full of elements, e.g :

std::array<int, 5> foo;
foo[0] = 4;
foo[1] = 2;
foo[2] = 3;

will foo.end() still point to the last element + 1 or will it point to the value at foo[3]?

My context is that I am trying to find the minimum value location of an array which only sometimes has all the elements assigned. Some example code is below:

int min_loc = std::min_element(foo.begin(), foo.end()) - foo.begin();

Will this extract min_loc = 1? Or will it find 0 at the undefined elements in the array and assign those as the minimum location?

If so, should I define the line above as:

int min_loc = std::min_element(foo.begin(), foo.end() - 1) - foo.begin();

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire