Re: Bug in my code or compiler?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 05/26/04
- Next message: joe: "Re: accelerated c++ chapter 4 error compiling"
- Previous message: Leor Zolman: "Re: Scope question"
- In reply to: Gary Labowitz: "Re: Bug in my code or compiler?"
- Next in thread: Gary Labowitz: "Re: Bug in my code or compiler?"
- Reply: Gary Labowitz: "Re: Bug in my code or compiler?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 26 May 2004 02:06:20 -0400
On Tue, 25 May 2004 23:31:44 -0400, "Gary Labowitz" <glabowitz@comcast.net>
wrote:
>>
>> I'm not sure what to think. Iterators are "restricted, generalized
>> pointers" (Scott Meyers' wording), so if it makes sense to see an iterator
>> as pointing before the element, it ought to make equal sense to see
>> pointers as pointing "before the object".
>>
>> Pointers are used to point to individual objects (not in an array or
>> vector, say) more often than iterators are (I guess that's why they're
>> called "iterators" and not "stlpointers" or something). But would it make
>> sense to think of a pointer to an individual object as pointing "before"
>> the object? I don't know, but I suspect that would cause more confusion
>> than your suggestion re. thinking about iterators could save.
>
>Hmmm.. this brings up a point I stuggle with. When you say "pointers point
>to ..." I feel like objecting.
Whoa. If you can't say "pointers point", then I'm afraid the sky would be
next to fall. Is nothing sacred? :-)
>I would rather say "pointers contain the address of ..."
>This has got to be more accurate.
Sorry, but those two are 100% equivalent to me, and I'd be hard-pressed to
admit to any conceivable distinction between them.
> As I understand it, when
>an iterator advances it makes current the element it advances over, but
>actually points past it to the next element that will be returned.
Doesn't make any sense to me, unless you're perhaps thinking of /reverse/
iterators, which do have this quaint way of pointing past (in a
right-to-left sort of way) the element they're actually referring to. If
you doubt that regular iterators actually point to (or "contain the address
of" the first byte of) their logical element, just consider the fact that
vector iterators are often implemented as raw pointers...
>That is
>why if you use a current element, then retreat one element (advance minus),
>then use the current element you get the same element both times.
Say, what? [Unless you're again dealing with reverse_iterators and getting
confused by some funky order of dereferencing, decrementing (or would it be
incrementing?), calling base(), and dereferencing again...OR you've
assigned a different meaning to "retreat" and "advance minus" than the way
I interpret it: as "decrement the iterator"]:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
int main()
{
using std::vector;
int i[] = {1,2,3};
vector<int> vi(&i[0], &i[3]);
vector<int>::iterator it = vi.begin() + 1;
cout << "*it = " << *it << endl;
--it;
cout << "after decrementing it, *it = " << *it << endl;
return 0;
}
Output:
*it = 2
after decrementing it, *it = 1
(and I would have been really, REALLY surprised to see otherwise)
> If you
>think of the iterator "pointing" to the element, this sequence could lead
>you to believe you will use the previous element to the one you used the
>first time. If this isn't true I'll have to go back to reading.
It may be time to hit the books ;-)
-leor
-- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
- Next message: joe: "Re: accelerated c++ chapter 4 error compiling"
- Previous message: Leor Zolman: "Re: Scope question"
- In reply to: Gary Labowitz: "Re: Bug in my code or compiler?"
- Next in thread: Gary Labowitz: "Re: Bug in my code or compiler?"
- Reply: Gary Labowitz: "Re: Bug in my code or compiler?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|