I have some questions about vector in STL to clarify…..
-
Where are the objects in vector allocated? heap?
-
does vector have boundary check? If the index out of the boundary, what error will happen?
-
Why array is faster than vector?
-
Is there any case in which vector is not applicable but array is a must?
vector<int>allocates memory the same waynew int[x]would.atmethod. It throws anstd::out_of_rangeexception if the boundary check fails. Theoperator[]doesn’t perform bounds checking.vectorif you want your container to have a dynamic size, and a simple array if a known fixed size is enough. Be sure to check out the other containers, likedequeandlist, to be sure you pick the most appropriate one. Otherwise, if you need to deal with non-C++ APIs, you’ll obviously need to have access to a regular array. (edit) @BillyONeal says you should use&vector[0]to get the address of the underlying array, but use it with care since it can change if the vector’s capacity changes.