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