mardi 3 mai 2016

Iterator to last element of std::vector using end()--

I have a std::vector and I want the iterator to the last element in the vector; I will be storing this iterator for later use.

NOTE: I want an iterator reference to it, not std::vector::back. Because I want to be able to compute the index of this object from the std::vector::begin later on.

The following is my logic to get the iterator to the last element:

std::vector<int> container;
std::vector<int>::iterator it = container.end()--;

Since std::vector::end has O(1) time complexity, is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire