For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2005 > Re: Newbie STL iterator question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Newbie STL iterator question
Bo Persson

2005-02-14, 9:11 pm


"Zardoz" <me@you.twang> skrev i meddelandet
news:n97iv0956ohsdg3if8537q6dfdth20l4h7@
4ax.com...
>
>
> I want to be able to use an iterator as a pointer to a structure
> stored in the vector.
>
> I've got a vector full of structures:
>
> vector<CardDef> m_vCards;
>
> and I want to access it / them, so
>
>
> for(vector<CardDef>::iterator i=m_vCards.begin(); i!=m_vCards.end();
> ++i)
> {
>
>
> (1) CardDef* p=(CardDef*)&i;
> (2) CardDef* q=(CardDef*)&m_vCards[lCount];
>
> TRACE("%i \n", p->lSequence);
> TRACE("%i \n", q->lSequence);
>
>
> lCount++;
> }
>
> q works OK.
>
> p always seems to be the same thing (i) (which is different from q)
> but i.current _does_ point to q.
>
>
> Is there something up with my casting in (1) ?


It's sort of backwards...

You can use the iterator to access the element of the vector

*i

if you want the address of the element, just use the address-of operator

&*i

You don't need the casts anyway, if you do it properly:

CardDef* p = &*i;

CardDef* q = &m_vCards[lCount];


Note that i->lSequence gives you the same result as p->lSequence or
q->lSequence though!


Bo Persson


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com