矢量是链表的特例吗?

发布于 2024-10-12 01:14:38 字数 190 浏览 2 评论 0原文

在谈论STL时,有几个同学告诉我“向量是链表”。

我还有另一个观点,如果你用迭代器调用擦除()方法,它会破坏向量,因为它是一个链接列表。

他们也往往不理解为什么我总是认为向量是连续的,就像任何其他数组一样,并且似乎不理解随机访问的含义。向量是像常规数组一样严格连续,还是最多连续? (例如,如果整个数组不适合,它将分配几个连续的段)。

When talking about the STL, I have several schoolmates telling me that "vectors are linked lists".

I have another one arguing that if you call the erase() method with an iterator, it breaks the vector, since it's a linked list.

They also tend to don't understand why I'm always arguing that vector are contiguous, just like any other array, and don't seem to understand what random access means. Are vector stricly contiguous just like regular arrays, or just at most contiguous ? (for example it will allocate several contiguous segments if the whole array doesn't fit).

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

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

发布评论

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

评论(4

孤云独去闲 2024-10-19 01:14:38

我很遗憾地告诉你,你的同学完全错了。如果你的同学可以诚实地说“向量是链表”,那么你需要恭敬地告诉他们,他们需要拿起一本好的 C++ 书籍(或任何像样的计算机科学书籍)并阅读它。或者甚至可能是 向量列表。 (另请参阅动态数组链表。)


向量(如 std::vector不是链表。 (请注意,std::vector 不是从 std::list 派生的)。虽然它们都可以存储数据集合,但向量的存储方式与链表的存储方式完全不同。因此,它们在不同的情况下具有不同的性能特点。

例如,插入是链表上的常量时间操作,而如果插入到末尾以外的其他位置,则它是向量上的线性时间操作。 (但是,如果在向量末尾插入,则它是摊销常量时间。)

C++ 中的 std::vector 类是必需的 按照 C++ 标准是连续的:

23.2.4/1 类模板向量

向量是一种支持随机访问迭代器的序列。此外,它还支持末尾(摊销)恒定时间插入和擦除操作;中间的插入和擦除需要线性时间。存储管理是自动处理的,但可以给出提示以提高效率。 向量的元素是连续存储的,这意味着如果v是一个向量,其中Tbool 以外的某种类型,那么它遵循恒等式 &v[n] == &v[0] + n对于所有 0 <= n < v.size().

std::list 进行比较:

23.2.2/1 类模板列表

列表是一种支持双向迭代器的序列,并允许在序列中的任何位置进行恒定时间插入和擦除操作,并自动处理存储管理。与向量(23.2.4)和双端队列(23.2.1)不同,不支持对列表元素的快速随机访问,但许多算法无论如何只需要顺序访问。

显然,C++ 标准规定向量和列表是两个不同的容器,它们的作用不同。

您不能通过简单地使用有效迭代器调用 erase() 来“破坏”向量(至少不是故意的)。这将使 std::vector 变得毫无用处,因为它存在的目的是为您管理内存!

I'm sorry to say that your schoolmates are completely wrong. If your schoolmates can honestly say that "vectors are linked lists" then you need to respectfully tell them that they need to pick up a good C++ book (or any decent computer science book) and read it. Or perhaps even the Wikipedia articles for vectors and lists. (Also see the articles for dynamic arrays and linked lists.)


Vectors (as in std::vector) are not linked lists. (Note that std::vector do not derive from std::list). While they both can store a collection of data, how a vector does it is completely different from how a linked list does it. Therefore, they have different performance characteristics in different situations.

For example, insertions are a constant-time operation on linked lists, while it is a linear-time operation on vectors if it is inserted in somewhere other than the end. (However, it is amortized constant-time if you insert at the end of a vector.)

The std::vector class in C++ are required to be contiguous by the C++ standard:

23.2.4/1 Class template vector

A vector is a kind of sequence that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficienty. The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

Compare that to std::list:

23.2.2/1 Class template list

A list is a kind of sequence that supports bidirectional iterators and allows constant time insert and erase operations anywhere within the sequence, with storage management handled automatically. Unlike vectors (23.2.4) and deques (23.2.1), fast random access to list elements is not supported, but many algorithms only need sequential access anyway.

Clearly, the C++ standard stipulates that a vector and a list are two different containers that do things differently.

You can't "break" a vector (at least not intentionally) by simply calling erase() with a valid iterator. That would make std::vectors rather useless since the point of its existence is to manage memory for you!

故人如初 2024-10-19 01:14:38

vector 会将其所有存储空间保存在一个位置。 向量 与链表一点也不像。事实上,如果我必须选择两个最不同的数据结构,那就是向量和列表。 “至多连续”是双端队列的运作方式。

Vector:

  1. 保证所有元素的连续存储 - 将复制或移动元素。
  2. O(1) 访问时间。
  3. O(n) 用于插入或删除。
  4. 迭代器在插入或删除任何元素时失效。

列表:

  1. 根本没有连续存储 - 从不复制或移动元素。
  2. O(n) 访问时间 - 加上您将遇到的所有令人讨厌的缓存未命中。
  3. O(1) 插入或删除。
  4. 只要特定元素未被删除,迭代器就有效。

正如您所看到的,它们在每个数据结构用例中的行为都不同。

vector will hold all of it's storage in a single place. A vector is not even remotely like a linked list. Infact, if I had to pick two data structures that were most unlike each other, it would be vector and list. "At most contiguous" is how a deque operates.

Vector:

  1. Guaranteed contiguous storage for all elements - will copy or move elements.
  2. O(1) access time.
  3. O(n) for insert or remove.
  4. Iterators invalidated upon insertion or removal of any element.

List:

  1. No contiguous storage at all - never copies or moves elements.
  2. O(n) access time- plus all the nasty cache misses you're gonna get.
  3. O(1) insert or remove.
  4. Iterators valid as long as that specific element is not removed.

As you can see, they behave differently in every data structure use case.

晒暮凉 2024-10-19 01:14:38

根据定义,向量是类似于 C 数组的连续内存块。请参阅:http://en.wikipedia.org/wiki/Vector_(C%2B) %2B)

向量允许随机访问;那是,
向量的元素可以是
以同样的方式引用
数组的元素(按数组索引)。
另一方面,链表和集合
手,不支持随机访问或
指针算术。

By definition, vectors are contiguous blocks of memory like C arrays. See: http://en.wikipedia.org/wiki/Vector_(C%2B%2B)

Vectors allow random access; that is,
an element of a vector may be
referenced in the same manner as
elements of arrays (by array indices).
Linked-lists and sets, on the other
hand, do not support random access or
pointer arithmetic.

怎樣才叫好 2024-10-19 01:14:38

向量不是链接的链表,它们提供随机访问并且像数组一样是连续的。为了实现这一目标,他们在后台重新分配内存。

列表的设计目的是允许快速插入和删除,同时不会使任何引用或迭代器无效,除了
那些到被删除的元素。

Vectors are not linked linked list, they provide random access and are contiguous just like arrays. In order to achieve this they re-allocate memory under the hood.

List is designed to allow quick insertions and deletions, while not invalidating any references or iterators except
the ones to the deleted element.

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