Some questions about Vector in STL
I have开发者_C百科 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?
- In a contiguous memory block on the heap. A
vector<int>
allocates memory the same waynew int[x]
would. - Only if you use the
at
method. It throws anstd::out_of_range
exception if the boundary check fails. Theoperator[]
doesn't perform bounds checking. - Because an array gives direct access to memory, while accessing a vector element most likely involves a method call. The difference can be ridiculously small though, especially if your compiler decides to inline the calls.
- In general, you'll use a
vector
if 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, likedeque
andlist
, 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.
Where are the objects in vector allocated? heap?
It depends on the STL implementation, but in all likelihood on the heap, yes.
does vector have boundary check? If the index out of the boundary, what error will happen?
Yes, a vector grows dynamically, you can check its size using the capacity()
member function. If it should run out of space, it generally allocates more space using the reserve()
member function.
Why array is faster than vector?
Arrays can be faster because they are plain data that does not need to be accessed through a wrapper object such as vector.
You can think of a vector
as neatly packaged array for your convenience.
Is there any case in which vector is not applicable but array is a must?
I think there can be times where an array is preferable over a vector
. For example, when dealing with legacy C code or when speed is of utmost importance. But generally you can solve any array problem by containing the data in an STL vector
.
Ad 4.: When dealing with legacy interfaces (e.g. POSIX) arrays may be a must.
On the heap (assuming you use the standard allocator, which is the default)
It is not boundary checked when using
operator[]
, but it is if you use the member functionat
(e.g.my_vec.at(0)
). If you useat
and the index is out or bounds, it throws anstd::out_of_range
exception.Arrays aren't generally faster. It depends whether or not the vector's
operator[]
calls are inlined or not. Most modern compilers should index it though, as it is just a single array index.In general, normal arrays can be replaced by vectors. You shouldn't really use a
std::vector
on the public interface of a library, and manu library APIs require raw C-style arrays.
By default contents are allocated dynamically. (But I suppose you can provide an allocator that gets the memory from some place else.)
The
at
method does bounds checks and throws anout_of_range
. Other methods may or may not check bounds, depending on implementation and settings.I would assume a vector to be slower than an array when it does something different from an array. For example, dynamic allocation is costly, so vector has a cost that arrays don't have. Dynamically resizing a vector is costly, whereas an array can't be resized at all. I wouldn't expect to see any difference when accessing the elements (except for possible run-time checks done by the implementation which you should be able to turn off if desired).
I don't know of such a scenario. You can always get a pointer to the underlying array of a vector with
&vec[0]
. However, a vector can be an overkill if you don't need any of the features it provides - mostly the ability to (re)size it dynamically. In such cases an array could do just fine, but note that there are classes that make an array a first-class object too (std::tr1::array
orboost::array
) which make the array copyable and not decay into a pointer.
精彩评论