473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

std::list::reverse_iterator not working right.

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
Jun 3 '06 #1
4 3104
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
Jun 3 '06 #2
Jim Langston wrote:
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

Jun 3 '06 #3

"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Jim Langston wrote:
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.


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!
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

Jun 3 '06 #4
Jim Langston wrote:
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Jim Langston wrote:
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.


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

Jun 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
11
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to...
8
by: emanshu | last post by:
Hi, i am storing data read from some file (text,pdf,gif...) into std::list using push_back function available for list. and my list type is string.( std::list<string> mylist) after storing...
25
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It...
3
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
11
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.