jeudi 1 août 2019

Why cant we use a pointer to traverse the vector?

I am trying to print all the elements in a vector using a pointer. What is wrong in the code?

#include <bits/stdc++.h>
using namespace std;
int main() {
    vector <int> v = {1,2,3};
    int * p;
    for(p=v.begin();p != v.end();p++)
        cout<<*p<<" ";

}

I get a compilation error.

Aucun commentaire:

Enregistrer un commentaire