| Jeff F 2005-02-14, 9:11 pm |
| Zardoz wrote:
> 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;
CardDef* p = &*i; // no need to cast here or later
> (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) ? Or am I barking up the
> wrong tree and might as well use (2) ?
As I said above there's no need to cast. The question is why do any of this
at all. You've already got an iterator, just do:
> TRACE("%i \n", i->lSequence);
> TRACE("%i \n", m_vCards[lCount].lSequence);
Jeff
|