iterate through 2D vector, cant dereference?

519 Views Asked by At

I am trying to initialize two iterators two my 2D vector, one for the rows and one for the columns. I have done it this way:

vector<vector<int> > v;
    vector<vector<int> >::iterator r;
    vector<int>::iterator c;

    r = v.begin();
    c = r->begin();

and i get the following pop-up window, when i run the code: Debug Assertion Failed! Expression: can't dereference value initialized vector iterator.

There are som problem with this statement:

c = r->begin();

But cant see why?

Thanks

1

There are 1 best solutions below

0
On

v is empty, so r doesn't point to a valid vector<int> instance (there is no instance to point to). You are essentially dereferencing v.end(), whereupon your program exhibits undefined behavior.