Guillaume
Guillaume

Reputation: 9071

Pointer on vector of vector and iterator

I have a vector of vector of my object and I get a pointer of this vector. My problem is I can't create an iterator with that. This my code :

vector<vector<AbstractBlock*>> *vectorMap = _level->getMap()->getVectorMap();

for(vector<AbstractBlock*>::iterator i = vectorMap[colonneX-1].begin(); i < vectorMap[colonneX-1].end(); i++)
{
    /*some operations*/
}

It failed on vectorMap[colonneX-1].begin(), if the vectorMap is not a pointer I can do this

How I can make this?

Thanks!

Upvotes: 1

Views: 276

Answers (3)

hmjd
hmjd

Reputation: 122011

Dereference vectorMap:

for(vector<AbstractBlock*>::iterator i = (*vectorMap)[colonneX-1].begin();
    i != (*vectorMap)[colonneX-1].end(); i++)

Upvotes: 3

Emilio Garavaglia
Emilio Garavaglia

Reputation: 20759

You mistake the number of indirection. But there may be two different correct meaning.

if vectormap is a pointer, vectormap[x] is the x-th vectormap in an hypothetical vector<vector<AbstractBlock*>> array.

I found strange that's what you mean, since it doesn't match the iterator type.

But *vectormap is a vector<vector<...>>, (*vectormap)[x] is a vector<AbstractBlock*>>, whose iterator, if dereferenced twice, is an AbstractBlock.

You most likely mean

for(vector<AbstractBlock*>::iterator i = (*vectorMap)[colonneX-1].begin();
    i != (*vectorMap)[colonneX-1].end(); i++)
    (**i).abstractblock_methodcall();

Upvotes: 0

MGZero
MGZero

Reputation: 5983

vectorMap is a pointer to a vector, not a vector. They are two different things. The pointer simply refers to the vector, they are not one in the same. You need to dereference vectorMap.

Upvotes: 0

Related Questions