Hello all, so I am trying to check for the last element in the list, but I don't know how. list.begin() is the first element, but list.end() is the one past the last element. Doing list.end()-- seems to give me an error. My thought of doing this is using a loop that will keep iterating until it hits the list.end(). When it does that I --iter to get the last element and initialize it as something else. However is there a shorter way of doing this?

04-02-2009

R.Stiltskin

What do you mean by "check for the last element"?

list.back() returns a reference to the last element. Is that what you want?

04-02-2009

dnguyen1022

It looks exactly like what I want, but for some reason I am getting an error with this piece of code.

error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

Thanks for your help!

04-02-2009

R.Stiltskin

Because phrase.back() is not an iterator, it's a const reference. What are you trying to do? Why are you iterating through the list? phrase.back() gives you direct access to the last item so you can do, for example

Code:

cout << phrase.back() << endl;

Or are you trying to alter it?

04-03-2009

iMalc

rbegin() is the last element, assuming the list is not empty, otherwise it will equal rend().
Are you wanting to loop over all items except the last one?

04-04-2009

dnguyen1022

Yes I am trying to loop through all the items except the last one.

If rbegin() is what I am looking for then this should work..but its not.