[Solved] Howto understand pointer on vector in C++


The declaration std::vector<int> * ids; says that this is a pointer to either a single object of type std::vector<int> or to (the first element of) an array of that type. The fact that operator[] is used on it in the member function shows that the second is the case.

Applying operator[] to a pointer (as in ids[item]) accesses an element (in this case, the element with number item) of the array the pointer points to. The fact that the type of the objects in the array (std::vector<int>) also has an operator[] defined doesn’t matter because this code does not call that (you could call operator[] on such an object by adding another indexing operator, like ids[item][2], or by dereferencing the pointer, like (*ids)[2] (which is equivalent to ids[0][2]).

2

solved Howto understand pointer on vector in C++