In case you want it to throw an exception, use std::vector::at
1 instead of operator[]
:
try {
agrid.at(-1).at(-1) = 5;
}
catch (const std::out_of_range& e) {
cout << "Out of Range error.";
}
1 - Returns a reference to the element at specified location pos
, with bounds checking. If pos
is not within the range of the container, an exception of type std::out_of_range
is thrown