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

Viewed 303268

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

8 Answers
vector <int> numbers {10,20,30,40};
vector <int> *ptr {nullptr};

ptr = &numbers;

for(auto num: *ptr){
 cout << num << endl;
}


cout << (*ptr).at(2) << endl; // 20

cout << "-------" << endl;

cout << ptr -> at(2) << endl; // 20
Related