Re: Last valid element in a vector



PGP schrieb:

vector::begin() will return the first element and returns value of type iterator. Is vector.begin() + vector.size()-1 the only way to get the last valid element in the vector ? I know rbegin() would return this but i need a method that returns a value of type iterator (just to keep the code shorter)

How about

vector<int> v;

v.push_back(1);
assert(!v.empty());
vector<int>::iterator e = v.end()-1;

Norbert
.



Relevant Pages

  • Re: Last valid element in a vector
    ... last valid element in the vector? ... I know rbegin() would return this but ... i need a method that returns a value of type iterator (just to keep the ... Note the assert as well - end-1 is not valid if the vector is empty. ...
    (microsoft.public.vc.stl)
  • Re: Last valid element in a vector
    ... last valid element in the vector? ... I know rbegin() would return this but i ... need a method that returns a value of type iterator (just to keep the code ... Stephen Howe ...
    (microsoft.public.vc.stl)
  • Re: Last valid element in a vector
    ... PGP wrote: ... Is vector.begin+ vector.size-1 the only way to get the last valid element in the vector? ... I know rbegin() would return this but i need a method that returns a value of type iterator ...
    (microsoft.public.vc.stl)
  • Re: Last valid element in a vector
    ... last valid element in the vector? ... I know rbegin() would return this but ... i need a method that returns a value of type iterator (just to keep the ... Didnt know end- 1 could be used. ...
    (microsoft.public.vc.stl)
  • Re: std::vector question
    ... > canonical way to traverse a vector backwards is to use a reverse ... > iterator, along with rbegin() and rend(): ...
    (alt.comp.lang.learn.c-cpp)

Loading