关于STL中Vector的一些问题
我有一些关于STL中向量的问题需要澄清......
向量中的对象分配在哪里?堆?
向量有边界检查吗?如果索引超出边界,会出现什么错误?
为什么数组比向量更快?
是否存在不适用向量而必须使用数组的情况?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
vector
分配内存的方式与new int[x]
相同。at
方法时。如果边界检查失败,它会抛出std::out_of_range
异常。operator[]
不执行边界检查。deque
和list
,以确保选择最合适的容器。否则,如果您需要处理非 C++ API,则显然需要访问常规数组。 (编辑)@BillyONeal 说您应该使用&vector[0]
来获取底层数组的地址,但要小心使用它,因为如果向量的容量发生变化,它可能会发生变化。vector<int>
allocates memory the same waynew int[x]
would.at
method. It throws anstd::out_of_range
exception if the boundary check fails. Theoperator[]
doesn't perform bounds checking.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.这取决于 STL 实现,但很可能取决于堆,是的。
是的,向量动态增长,您可以使用
capacity()
成员函数检查其大小。如果空间不足,它通常会使用reserve()
成员函数分配更多空间。数组可以更快,因为它们是纯数据,不需要通过包装对象(例如向量)访问。您可以将向量视为整齐打包的数组。方便。
我认为有时数组比向量更可取。例如,在处理遗留 C 代码或速度至关重要时。但通常您可以通过将数据包含在 STL
向量
中来解决任何数组问题。It depends on the STL implementation, but in all likelihood on the heap, yes.
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 thereserve()
member function.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 avector
as neatly packaged array for your convenience.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 STLvector
.Ad 4.:在处理遗留接口(例如 POSIX)时,数组可能是必须的。
Ad 4.: When dealing with legacy interfaces (e.g. POSIX) arrays may be a must.
在堆上(假设您使用标准分配器,这是默认的)
使用 < 时不会进行边界检查code>operator[],但如果您使用成员函数
at
(例如my_vec.at(0)
)。如果您使用at
并且索引超出或超出范围,则会抛出std::out_of_range
异常。数组通常不会更快。这取决于向量的
operator[]
调用是否内联。不过,大多数现代编译器应该对其进行索引,因为它只是单个数组索引。一般来说,普通数组可以用向量代替。您实际上不应该在库的公共接口上使用
std::vector
,并且 manu 库 API 需要原始 C 样式数组。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.默认情况下,内容是动态分配的。 (但我想您可以提供一个分配器,从其他地方获取内存。)
at 方法进行边界检查并抛出
out_of_range
。其他方法可能会也可能不会检查边界,具体取决于实现和设置。当向量执行与数组不同的操作时,我会假设向量比数组慢。例如,动态分配的成本很高,因此向量具有数组所没有的成本。动态调整向量的大小成本很高,而数组则根本无法调整大小。我不希望在访问元素时看到任何差异(除了实现可能进行的运行时检查,如果需要,您应该能够关闭它)。
我不知道有这样的场景。您始终可以使用
&vec[0]
获取指向向量底层数组的指针。但是,如果您不需要它提供的任何功能(主要是动态(重新)调整其大小的能力),则矢量可能会显得过大。在这种情况下,数组可以做得很好,但请注意,有些类也使数组成为一流对象(std::tr1::array
或boost::array< /code>) 使数组可复制并且不会衰减为指针。
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.