Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the contents of a vector from a pointer to the vector in C++?

I have a pointer to a vector. Now, how can I read the contents of the vector through pointer?

like image 954
Pavan Dittakavi Avatar asked Aug 04 '11 17:08

Pavan Dittakavi


1 Answers

There are many solutions, here's a few I've come up with:

int main(int nArgs, char ** vArgs) {     vector<int> *v = new vector<int>(10);     v->at(2); //Retrieve using pointer to member     v->operator[](2); //Retrieve using pointer to operator member     v->size(); //Retrieve size     vector<int> &vr = *v; //Create a reference     vr[2]; //Normal access through reference     delete &vr; //Delete the reference. You could do the same with                 //a pointer (but not both!) } 
like image 64
Seb Holzapfel Avatar answered Sep 18 '22 13:09

Seb Holzapfel