I know this is probably a really simple question, but how do you create a pointer to a vector? Sort of like this:
[code]
std::vector<int> normalvec;
std::vector<int> * ptrvec;
ptrvec = &normalvec;
ptrvec.at(...) // Access elements of normalvec
[/code]
I tried something like this and obviously it doesn't work as I was just taking a wild stab in the dark. I tried searching on Google but I only got results about vectors of type int*, etc. Hope you can help.
That's exactly how you do it. However, when you access the pointed-to-object's members using the pointer, you're supposed to do "ptrvec->at(...)".
EDIT: Also, you'll probably want to have your vector on the heap. You'd do that by removing the first line, and changing the second one to "std::vector<int> * ptrvec = new std::vector<int>;" Don't forget to delete what you new. If you don't understand what I'm talking about, find a good book.
Thanks, I'll try that in a second. Also, what advantages will declaring the vector on the heap have.
[b]EDIT:[/b]
It worked. Looking back on five minutes ago, I realize how stupid I was.
I can't think of any situations where really need this indirection. std::vector is itself a reference type, so it has constant size. You can easily declare a vector on the stack and use them for arguments and everything and the content of the vector is the same and unaffected.
std::vector is effectively just a pointer to an array of data out on the heap, and a ssize_t to specify the size of the vector.
Sorry, you need to Log In to post a reply to this thread.