vector question

whats the difference between these
std::vector<int *> tmp vs std::vector<int> *tmp?
a vector of pointers, and a pointer to a vector.

the vector's type is inside the related <> pair.
a * pointer symbol modifies the left item, making it a pointer:
int *x; //x is of type int pointer
foo *x;//x is of type foo pointer
vector<foo> * x; //x of type vector pointer, and the vector is of type foo, or putting all
those together the type is pointer to vector of foos, is the best way to say it. The english comes off a little out of order to the C++ in that case; trying to say it left to right sounds off (vector of foo pointer is OK, but out loud, unclear if its vector of foo or vector of *foo so better to reorder the words for clarity)

or the other one
vector <*foo> x; //x is a vector of pointers to foos, or, THIS is what I think of when you say "vector of foo pointers" as above, though it remains unclear.
Last edited on
thx, so if i do something like this

vector<int> *tt;
vector<int*> ttx;
vector<int> ttt;


for (int i=0; i<2; i++)
{
ttt.push_back(i);
ttx.push_back(&ttt.front());
}
tt=&ttt;
tt++;


is this mean ttx store address of ttt.front, and how do i display tt content? is tt increment by entire sizeof ttt?
ttx has a bunch of pointers to the first element, because you loaded it in the loop. if you had used back instead, it would have a pointer to each element.

tt only has one valid entry, so using it after incrementing it is an error. tt is incremented by the size of ttt, but that isn't very large, since vectors store their data in a pointer (size 8 bytes typically). Try sizeof() on a vector sometime, its tiny.

*tt[0], *tt[1] ... one way to print tt's contents. You can rig up all kinds of ways to print the vector under tt, though.
edit -- that may need () or something since pointer and vector both have a [] operator, it could hate on it. But generally some sort of dereference tt and access that as a vector. even tt->operator[](0) if you want.
Last edited on
Incrementing a pointer (using ++) only really makes sense if it points to an element in an array (or vector). It makes the pointer point to the next element in the array. There is no reason to increment tt because it doesn't point to an array element.

Note that vectors sometimes need to move its elements to a new location in memory. This can happen when you add a new element and the array where the vector stores its elements is too small. If that happens it will have to allocate a new larger array and copy/move the elements to the new array. So you need to be careful when storing pointers to vector elements. It's very likely that the first element of ttx (ttx[0]) is now "dangling" (i.e. it points to memory that has now been released).
Last edited on
*tt[0], *tt[1] ... one way to print tt's contents.

No that's not how you do it. *tt[0] means *(tt[0]), i.e. access the first vector and then try to dereference it. Vectors don't have a dereference operator so it will give you a compilation error.

that may need () or something since pointer and vector both have a [] operator, it could hate on it. But generally some sort of dereference tt and access that as a vector. even tt->operator[](0) if you want.

No need to complicate things. Just dereference the pointer inside parentheses before accessing the vector element.

1
2
3
4
for (size_t i = 0; i < tt->size(); i++)
{
	cout << (*tt)[i] << '\n';
}


Note that tt->size() is a shorthand syntax for (*tt).size().
Last edited on
And the elephant in the room is: why have a pointer to a vector (at all), especially if it is just to print the contents? It looks like the anti pattern of C style thinking applied to C++ code.
thanks for that. From time to time I have days where I just can't get the right words/syntax to come out.

pointer to vector is going to be rare. Ive used such to sort 2-d when the user can change what field to sort on frequently, or if you don't want to modify original.
Topic archived. No new replies allowed.