mikbal
mikbal

Reputation: 1198

two dimensional vector access

whats the difference between these two access methods?

nodes->at(235).push_back(NavigationNode(NULL,0,0));

nodes[235].push_back(NavigationNode(NULL,0,0));

second one gives this compile error

cannot convert parameter 1 from 'PathFinder::NavigationNode' to 'const std::vector<_Ty> &'

i'm very confused about this error

Upvotes: 0

Views: 242

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272762

It depends whether nodes is a vector or a vector * (or an iterator).

If the first one compiles, then it must be a vector * (or an iterator). In which case the second one would need to become:

(*nodes)[235].push_back(NavigationNode(NULL,0,0));

Note, however, that accessing via operator[] and via at() have different semantics. The latter will do a bounds check.

Upvotes: 5

Related Questions