Why I don't get an exception when using operator [] with index out of range in std::vector?
问题 Why when I use below code I don't get an out of range exception ? std::vector<int> v; v.resize(12); int t; try { t = v[12]; } catch(std::exception e){ std::cout<<"error:"<<e.what()<<"\n"; } 回答1: By using operator[] you are essentially telling the compiler "I know what I'm doing. Trust me." If you access some element that is outside of the array it's your fault. You violated that trust; you didn't know what you were doing. The alternative is to use the at() method. Here you are asking the