关于STL中Vector的一些问题

发布于 2024-08-25 14:38:41 字数 188 浏览 0 评论 0原文

我有一些关于STL中向量的问题需要澄清......

  1. 向量中的对象分配在哪里?堆?

  2. 向量有边界检查吗?如果索引超出边界,会出现什么错误?

  3. 为什么数组比向量更快?

  4. 是否存在不适用向量而必须使用数组的情况?

I have some questions about vector in STL to clarify.....

  1. Where are the objects in vector allocated? heap?

  2. does vector have boundary check? If the index out of the boundary, what error will happen?

  3. Why array is faster than vector?

  4. Is there any case in which vector is not applicable but array is a must?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傻比既视感 2024-09-01 14:38:41
  1. 在堆上的连续内存块中。 vector 分配内存的方式与 new int[x] 相同。
  2. 仅当您使用 at 方法时。如果边界检查失败,它会抛出 std::out_of_range 异常。 operator[] 不执行边界检查。
  3. 因为数组可以直接访问内存,而访问向量元素很可能涉及方法调用。不过,差异可能小得离谱,特别是如果您的编译器决定内联调用。
  4. 一般来说,如果您希望容器具有动态大小,则可以使用向量;如果已知的固定大小就足够了,则可以使用简单数组。请务必检查其他容器,例如 dequelist,以确保选择最合适的容器。否则,如果您需要处理非 C++ API,则显然需要访问常规数组。 (编辑)@BillyONeal 说您应该使用 &vector[0] 来获取底层数组的地址,但要小心使用它,因为如果向量的容量发生变化,它可能会发生变化。
  1. In a contiguous memory block on the heap. A vector<int> allocates memory the same way new int[x] would.
  2. Only if you use the at method. It throws an std::out_of_range exception if the boundary check fails. The operator[] doesn't perform bounds checking.
  3. 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.
  4. 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, like deque and list, 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.
瑾兮 2024-09-01 14:38:41

向量中的对象在哪里
分配?堆?

这取决于 STL 实现,但很可能取决于堆,是的。

向量有边界检查吗?如果索引超出边界, 
会发生什么错误?

是的,向量动态增长,您可以使用capacity()成员函数检查其大小。如果空间不足,它通常会使用 reserve() 成员函数分配更多空间。

为什么数组比向量更快?

数组可以更快,因为它们是纯数据,不需要通过包装对象(例如向量)访问。您可以将向量视为整齐打包的数组。方便。

有没有向量是的情况
不适用但数组是必须的?

我认为有时数组比向量更可取。例如,在处理遗留 C 代码或速度至关重要时。但通常您可以通过将数据包含在 STL 向量 中来解决任何数组问题。

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.

你列表最软的妹 2024-09-01 14:38:41

Ad 4.:在处理遗留接口(例如 POSIX)时,数组可能是必须的。

Ad 4.: When dealing with legacy interfaces (e.g. POSIX) arrays may be a must.

还给你自由 2024-09-01 14:38:41
  1. 在堆上(假设您使用标准分配器,这是默认的)

  2. 使用 < 时不会进行边界检查code>operator[],但如果您使用成员函数 at(例如 my_vec.at(0))。如果您使用 at 并且索引超出或超出范围,则会抛出 std::out_of_range 异常。

  3. 数组通常不会更快。这取决于向量的 operator[] 调用是否内联。不过,大多数现代编译器应该对其进行索引,因为它只是单个数组索引。

  4. 一般来说,普通数组可以用向量代替。您实际上不应该在库的公共接口上使用 std::vector,并且 manu 库 API 需要原始 C 样式数组。

  1. On the heap (assuming you use the standard allocator, which is the default)

  2. It is not boundary checked when using operator[], but it is if you use the member function at (e.g. my_vec.at(0)). If you use at and the index is out or bounds, it throws an std::out_of_range exception.

  3. 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.

  4. 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.

云巢 2024-09-01 14:38:41
  1. 默认情况下,内容是动态分配的。 (但我想您可以提供一个分配器,从其他地方获取内存。)

  2. at 方法进行边界检查并抛出 out_of_range。其他方法可能会也可能不会检查边界,具体取决于实现和设置。

  3. 当向量执行与数组不同的操作时,我会假设向量比数组慢。例如,动态分配的成本很高,因此向量具有数组所没有的成本。动态调整向量的大小成本很高,而数组则根本无法调整大小。我不希望在访问元素时看到任何差异(除了实现可能进行的运行时检查,如果需要,您应该能够关闭它)。

  4. 我不知道有这样的场景。您始终可以使用 &vec[0] 获取指向向量底层数组的指针。但是,如果您不需要它提供的任何功能(主要是动态(重新)调整其大小的能力),则矢量可能会显得过大。在这种情况下,数组可以做得很好,但请注意,有些类也使数组成为一流对象(std::tr1::arrayboost::array< /code>) 使数组可复制并且不会衰减为指针。

  1. By default contents are allocated dynamically. (But I suppose you can provide an allocator that gets the memory from some place else.)

  2. The at method does bounds checks and throws an out_of_range. Other methods may or may not check bounds, depending on implementation and settings.

  3. 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).

  4. 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 or boost::array) which make the array copyable and not decay into a pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文