Search code examples
c++vectorstlindexingpush-back

Does std::vector size()-1 ALWAYS give the index of the last element?


Say I have a std::vector named myVec.

I don't know the exact size it will reach, so i use only myVec.push_back(...).

Can I rely on the fact that after I have pushed_back, size()-1 will give me the index of that last element? Thanks.


Solution

  • For non-empty vectors yes, size()-1 is the index of the last element.