std::list::reverse_iterator not working right.

J

Jim Langston

Expected output is:
Three
One
One

Actual output is:
Three
One
Two

If I comment out the block that displays the first One, the last line is
blank (in real program it causes memory fault)

#include <string>
#include <iostream>
#include <list>

std::list< std::string > SendHistory;
std::list< std::string >::reverse_iterator SHi;

int main ()
{

SendHistory.push_back( "One" );
SendHistory.push_back( "Two" );
SendHistory.push_back( "Three" );

SHi = SendHistory.rbegin();
std::cout << (*SHi) << std::endl;

// Comment out following three lines changes output
SHi = SendHistory.rend();
--SHi;
std::cout << (*SHi) << std::endl;

// This is the line that's causing problems. Why isn't it working?
SHi-- = SendHistory.rend();
std::cout << (*SHi) << std::endl;

std::string wait;
std::cin >> wait;
}

The line that is causing the problems is:
SHi-- = SendHistory.rend();

Why is that failing? It should be the same as
SHi = SendHistory.rend();
--SHi;

but it's not working the same way. Is this a fault in my compiler maybe?
Microsoft Visual C++ .net 2003
 
K

Kai-Uwe Bux

Jim Langston wrote:
[snip]
The line that is causing the problems is:
SHi-- = SendHistory.rend();

Why is that failing? It should be the same as
SHi = SendHistory.rend();
--SHi;

Where did you read that?


Best

Kai-Uwe Bux
 
J

Jonathan Mcdougall

Jim said:
The line that is causing the problems is:
SHi-- = SendHistory.rend();

You may think this means "assign the result of rend() so SHi and then
decrement". People seem to confuse the use of postfix operator--, for
example with for loops, although this is the first time I see something
like that.

SHi-- decrements the iterator, but you then lose that value because you
are assigning the result of SendHistory.rend() to it.
Why is that failing? It should be the same as
SHi = SendHistory.rend();
--SHi;
but it's not working the same way. Is this a fault in my compiler maybe?
Microsoft Visual C++ .net 2003

Do not be to quick to blame your compiler. It very improbable for a
well-known commercial compiler to barf on such trivial statements.


Jonathan
 
J

Jim Langston

Jonathan Mcdougall said:
You may think this means "assign the result of rend() so SHi and then
decrement". People seem to confuse the use of postfix operator--, for
example with for loops, although this is the first time I see something
like that.

SHi-- decrements the iterator, but you then lose that value because you
are assigning the result of SendHistory.rend() to it.

Oh man, you're totally right. This totally excaped me. I guess because I
often do things such as:

bool result = MyFunction( parm1, SH1++, parm2 );

that I just always thought of SH1 not getting incremented until after the
statement was complete, but this is not really true. Hmmm... I better make
sure I'm not doing this anywhere else in my program!

Maybe a search for "++ =" and "-- =" will let me know.

Thanks!
 
J

Jonathan Mcdougall

This is wrong, see below.
Oh man, you're totally right. This totally excaped me. I guess because I
often do things such as:

bool result = MyFunction( parm1, SH1++, parm2 );

that I just always thought of SH1 not getting incremented until after the
statement was complete, but this is not really true.

Not really, no :)

SH1 (or was that SHi?) is incremented before the function is called.
However, what MyFunction() receives as is second parameters is the
original value, not the incremented one (assuming operator++ follows
the usual semantics).

Note that there is no magic here, it is only because postfix operator++
returns the original value. A valid (but silly) postfix operator++
could return the incremented value, or anything else for that matter.
Hmmm... I better make
sure I'm not doing this anywhere else in my program!

Maybe a search for "++ =" and "-- =" will let me know.

A statement such as

SHi-- = SendHistory.rend();

is misleading. SHi-- decrements SHi but returns a temporary on which
operator= is called. What you are doing here is assigining the value of
rend() to a temporary which gets destroyed at the end of the statement.
SHi retains its decremented value.

However,

--SHi = SendHistory.rend();

is undefined behavior (and will probably crash) because --SHi returns a
reference to SHi. That means rend() is assigned SHi which you try to
output later on.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top