c++ - Catching out_of_range on a vector of vectors -


i have vector of vectors establish map of integers, , love catch vector out of range error whenever thrown, doing following:

vector< vector<int> > agrid(sizex, vector<int>(sizey));  try {     agrid[-1][-1] = 5;     //throws out-of-range } catch (const std::out_of_range& e) {     cout << "out of range error."; } 

however, code doesn't seem catching error @ all. still seems want run std::terminate. know whats this?

in case want throw exception, use std::vector::at1 instead of operator[]:

try {     agrid.at(-1).at(-1) = 5; } catch (const std::out_of_range& e) {     cout << "out of range error."; } 

1 - returns reference element @ specified location pos, bounds checking. if pos not within range of container, exception of type std::out_of_range thrown


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -