473,386 Members | 1,715 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,386 software developers and data experts.

reverse_iterator::base, possible bug?

Hello,

In playing with a reverse_iterator issue today, I realized that I could use
its base() method to get at the underlying iterator. However, I realized
that in the implementation shipping with VC7.1 the base() method returns
"current", the internal iterator object, but that "current" iterator is
always set up to be "pointing" at the element AFTER the one you think.

For instance in <xutility> operator*() is defined as:

reference operator*() const
{
_RanIt _Tmp = current;
return *--_Tmp;
}

So if one uses the base() method you get something unexpected, i.e. you get
"current", which must be decremented before dereferenced to get the element
value you want.

At least, it seems unexpected if one reads section 16.3.2 of Stroustrup C++
3rd Ed. (page 445 in soft cover version). There he has an example of a
find_last method that reads:

template <class C>
typename C::iterator find_last( const C& c, typename C::value_type v )
{
return find_first( c.rbegin(), c.rend(), v ).base();
}

which seems to imply that base() should return the iterator to the element
found by find_first, not the one after the element found.

Basically, the question is: Is the shipping implementation correct? I
assume it is, but then I don't understand how the example from Stroustrup is
supposed to work.

Thanks,

-Eric Twietmeyer
Nov 17 '05 #1
3 1378
Eric Twietmeyer wrote:
Hello,

In playing with a reverse_iterator issue today, I realized that I could use
its base() method to get at the underlying iterator. However, I realized
that in the implementation shipping with VC7.1 the base() method returns
"current", the internal iterator object, but that "current" iterator is
always set up to be "pointing" at the element AFTER the one you think.

For instance in <xutility> operator*() is defined as:

reference operator*() const
{
_RanIt _Tmp = current;
return *--_Tmp;
}

So if one uses the base() method you get something unexpected, i.e. you get
"current", which must be decremented before dereferenced to get the element
value you want.
That's the way reverse_iterator::base is supposed to work.
At least, it seems unexpected if one reads section 16.3.2 of Stroustrup C++
3rd Ed. (page 445 in soft cover version). There he has an example of a
find_last method that reads:

template <class C>
typename C::iterator find_last( const C& c, typename C::value_type v )
{
return find_first( c.rbegin(), c.rend(), v ).base();
}

which seems to imply that base() should return the iterator to the element
found by find_first, not the one after the element found.

Basically, the question is: Is the shipping implementation correct? I
assume it is, but then I don't understand how the example from Stroustrup is
supposed to work.


That was fixed in an erratum:

Errata for 5th printing of The C++ Programming Language
http://www.research.att.com/~bs/3rd_printing6.html

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:1j********************************@4ax.com...

<snip>

That was fixed in an erratum:

Errata for 5th printing of The C++ Programming Language
http://www.research.att.com/~bs/3rd_printing6.html
Thanks for the pointer! I was quite confused by this. I'll try to look in
the
standard and see what it says about "base". Thanks again.

--
Doug Harrison
Microsoft MVP - Visual C++


-Eric Twietmeyer

Nov 17 '05 #3
Eric Twietmeyer wrote:
Thanks for the pointer! I was quite confused by this. I'll try to
look in the
standard and see what it says about "base". Thanks again.


Also look in Scott Meyer's "Effective STL" for a lengthy discussion of the
bizzare behavior of reverse_iterator.

-cd
Nov 17 '05 #4

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

Similar topics

0
by: praetorian | last post by:
high all i iterate my list with reverse_iterator and at some point i decide that certain element needs to be erased. how do i do that ? this is the code i tried inside iteration loop (_it is...
3
by: ben | last post by:
If I have a reverse_iterator to an element, does the standard library provide a way to find the iterator equivalent or do I have to roll my own?
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...
4
by: Denis Petronenko | last post by:
Why do i have segmentaion fault after executing str.erase(it.base()) ? compiler gcc 3.3.6. string TrimRight(const string& strin) { string str = strin; string::reverse_iterator it =...
3
by: noone | last post by:
string operator()(const bool clean=true) { string rv; MPEGQUEUE::reverse_iterator i=thequeue.rbegin(); MPEGQUEUE::reverse_iterator t=thequeue.rend(); while (i!=thequeue.rend()) { if...
1
by: wolverine | last post by:
Hi, I have read that to erase an element from a vector with reverse_iterator we have to use -- vector.erase( (++reverseItr).base()) -- But assuming i have to delete the first element of the...
20
by: Jess | last post by:
Hello, I think the two reverse_iterators are the same, except that the const_ version doesn't allow me to change the value pointed to by the iterators. However, I have a program that works for...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.