Re: find with reverse_iterators
Re: find with reverse_iterators
- Subject: Re: find with reverse_iterators
- From: Howard Hinnant <email@hidden>
- Date: Mon, 13 Nov 2006 17:20:42 -0500
On Nov 13, 2006, at 4:57 PM, Steve Mills wrote:
Have I totally forgotten some simple STL knowledge, or is something
wrong here? std::find seems to be giving back the wrong
reverse_iterator when given std::find(list.rbegin(), list.rend(),
obj). It should be returning item 1 but is returning item 2. I've
verified that I am correct by writing my own search loop:
vector<CTextLine*>::reverse_iterator startItR, startItR2;
CTextLine* line = FindTextLine(startChar);
for(LONG i = numLines - 1; i >= 0; i--)
if(m_LineList[i] == line) {
startItR = vector<CTextLine*>::reverse_iterator(m_LineList.begin
() + i);
break;
}
startItR2 = std::find(m_LineList.rbegin(), m_LineList.rend(), line);
check(*startItR == *startItR2);
The check assertion WILL fire. It is OK to send reverse_iterators
to find, right? As far as I can remember, it is because they know
how to act like forward iterators. Stepping into the calls I
checked all of the files (stl_algo, stl_vector, etc) and they have
a modification date of 9/11/2006 (I smell a conspiracy!;).
(Actually, all the files in /Developer/SDKs/MacOSX10.4u.sdk/usr/
include/c++/4.0.0/bits/ have the same date.) Were those files
changed in the Xcode 2.4.1 update from 2.4?
Believe it or not you got startItR2 right and startItR wrong. When
you say:
startItR = vector<CTextLine*>::reverse_iterator(m_LineList.begin
() + i);
*startItR does not refer to the same element as *(m_LineList.begin()
+ i). *startItR refers to the element prior to (m_LineList.begin() +
i). This is why you can say something like reverse_iterator
(mLineList.end()) *and* dereference that reverse_iterator without
decrementing it first.
Something to try:
vector<CTextLine*>::reverse_iterator startItR2;
vector<CTextLine*>::iterator startIt;
CTextLine* line = FindTextLine(startChar);
for(LONG i = numLines - 1; i >= 0; i--)
if(m_LineList[i] == line) {
startIt = m_LineList.begin() + i;
break;
}
startItR2 = std::find(m_LineList.rbegin(), m_LineList.rend(), line);
check(*startIt == *startItR2);
-Howard
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden