I have a std::vector. I check its size which is 6 but when I try to access vec[6] to check whether it will give error, I get no error but some number instead. Should not it give an error?
edit: something like:
struct Element
{
std::vector<double> face;
};
int main()
{
Element elm;
.... // insert 6 elements into elm.face
std::cout << elm.face.size() << std::endl; // answer is 6
std::cout << elm.face[6] << std::endl; // answer is some number
}
vec[5]starts with 0 – Bill May 18 '13 at 2:56vectorwould throw an error on an invalid index usingoperator[]. The docs certainly say otherwise. – Ed S. May 18 '13 at 2:57Notice that the first element has a position of 0 (not 1).-- cplusplus.com/reference/vector/vector/operator[] – Bill May 18 '13 at 2:58at()is for – Ed S. May 18 '13 at 3:00