矢量是链表的特例吗?
在谈论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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我很遗憾地告诉你,你的同学完全错了。如果你的同学可以诚实地说“向量是链表”,那么你需要恭敬地告诉他们,他们需要拿起一本好的 C++ 书籍(或任何像样的计算机科学书籍)并阅读它。或者甚至可能是 向量 和 列表。 (另请参阅动态数组和链表。)
向量(如
std::vector
)不是链表。 (请注意,std::vector
不是从std::list
派生的)。虽然它们都可以存储数据集合,但向量的存储方式与链表的存储方式完全不同。因此,它们在不同的情况下具有不同的性能特点。例如,插入是链表上的常量时间操作,而如果插入到末尾以外的其他位置,则它是向量上的线性时间操作。 (但是,如果在向量末尾插入,则它是摊销常量时间。)
C++ 中的
std::vector
类是必需的 按照 C++ 标准是连续的:与
std::list
进行比较:显然,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 thatstd::vector
do not derive fromstd::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:Compare that to
std::list
: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 makestd::vector
s rather useless since the point of its existence is to manage memory for you!vector
会将其所有存储空间保存在一个位置。向量
与链表一点也不像。事实上,如果我必须选择两个最不同的数据结构,那就是向量和列表。 “至多连续”是双端队列的运作方式。Vector:
列表:
正如您所看到的,它们在每个数据结构用例中的行为都不同。
vector
will hold all of it's storage in a single place. Avector
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 bevector
andlist
. "At most contiguous" is how adeque
operates.Vector:
List:
As you can see, they behave differently in every data structure use case.
根据定义,向量是类似于 C 数组的连续内存块。请参阅:http://en.wikipedia.org/wiki/Vector_(C%2B) %2B)
By definition,
vector
s are contiguous blocks of memory like C arrays. See: http://en.wikipedia.org/wiki/Vector_(C%2B%2B)向量不是链接的链表,它们提供随机访问并且像数组一样是连续的。为了实现这一目标,他们在后台重新分配内存。
列表的设计目的是允许快速插入和删除,同时不会使任何引用或迭代器无效,除了
那些到被删除的元素。
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.