Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector subscript out of range during compiling

Tags:

c++

int i = 0;
int j = 0;

vector<vector<int>> normal;
vector< vector<int> >::iterator row;
vector<int>::iterator col;

for (i = 1; i < 10; i++) {
    for (j = 1; j < 10; j++) {
         normal[i].push_back(j);

    }
}

Can someone explain me what l am doing wrong? during my compiling I got the error "Vector subscript out of range"

like image 367
pat des pat Avatar asked Dec 27 '19 17:12

pat des pat


People also ask

What does vector subscript out of range mean?

vector subscript out of range means "you asked for an element from the vector, but the vector is not of that size". e.g. vector<int> vec; vec.push_back(42); vec[1] // oops, 42 is vec[0]

What is the vector in C++?

Vectors in C++ are sequence containers representing arrays that can change their size during runtime. They use contiguous storage locations for their elements just as efficiently as in arrays, which means that their elements can also be accessed using offsets on regular pointers to its elements.


1 Answers

You never add any elements to normal before trying to use normal[i].

like image 124
Scott Hunter Avatar answered Sep 21 '22 11:09

Scott Hunter