Table of contents
To access an element in a vector using its index, we have two possible options
a. Subscript operator []
std::cout<< myvector[3] ;
b. at method from std::vector class
std::cout<<myvector.at(3);
Though both look identical i.e. to accomplish the task of reading an element from given index, we are better off using the latter. The Reason has to do with the behavior of the system when an out of bounds access is done
While using [] operator, we receive a crash with core dump due to invalid memory access, without any supporting diagnostic message. Beware, seemingly innocuous situations like these have known to cause multiple hours of debugging, before finally finding the offending code
In the same situation, if we've used at method, it will generate a legible error message through the exception handling done in implementation