简体   繁体   English

C ++中的双链表,链表和动态数组

[英]Doubly Linked-List, Linked-lists and dynamic arrays in C++

I'm new to C++ and probably these questions are so simple and basic [and sorry for that!]. 我是C ++的新手,可能这些问题是如此简单和基本(对此深表歉意!)。 Here I have two basic questions: 这里我有两个基本问题:

  1. As I understand vectors in C++ are dynamic arrays. 据我了解,C ++中的向量是动态数组。 They can change size by pushing back/front elements into them. 他们可以通过将后/前元素推入其中来更改大小。

    My question is: Are vectors allocated dynamically or the "dynamic array" name is just called because of the changing size capability? 我的问题是:由于大小能力的变化,是动态分配矢量还是只是调用“动态数组”名称? dynamically allocated array and dynamic arrays make me completely confused! 动态分配的数组和动态数组让我完全困惑!

  2. STL lists are linked-lists in C++ (again, as I understand). STL列表是C ++中的链接列表(据我所知)。 Are these doubly-linked lists or singly-linked list? 这些是双链表还是单链表?

Thanks, 谢谢,

Vectors are allocated dynamically, as you can see in the mentioned documentation . 向量是动态分配的,如您在上述文档中所见。 You can see this better in push_back function: 您可以在push_back函数中更好地看到这一点:

If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. 如果新的size()大于Capacity(),则所有迭代器和引用(包括过去的迭代器)都将失效。 Otherwise only the past-the-end iterator is invalidated. 否则,只有过去的迭代器是无效的。

Also, stick this in your mind which its complexity is amortized constant (as an instance, you can see this ). 另外,请记住它的复杂性是摊销的常量(例如,您可以看到 )。

For more info (mentioned in comments), you can find the real list as forward_list : 有关更多信息(在注释中提到),您可以将真实列表找到为forward_list

It is implemented as a singly-linked list and essentially does not have any overhead compared to its implementation in C. Compared to std::list this container provides more space efficient storage when bidirectional iteration is not needed. 它以单链列表的形式实现,与其在C中的实现相比,基本上没有任何开销。与std :: list相比,此容器在不需要双向迭代时提供了更节省空间的存储。

Also, as mentioned in the documentation , list is implemented as a doubly linked list. 另外,如文档中所述,列表被实现为双向链接列表。

List containers are implemented as doubly-linked lists ; 列表容器被实现为双向链接列表 Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. 双链列表可以将它们包含的每个元素存储在不同且不相关的存储位置。 The ordering is kept internally by the association to each element of a link to the element preceding it and a link to the element following it. 通过与到它前面的元素的链接和到它后面的元素的链接的每个元素的关联,在内部保持排序。

  1. Are vectors allocated dynamically or the "dynamic array" name is just called because of the changing size capability? 是由于大小能力的变化而动态分配矢量还是仅调用“动态数组”名称?

As far as I know, vectors are called dynamic arrays because their size can change at run time. 据我所知,向量被称为动态数组,因为它们的大小可以在运行时改变。 But indeed, they allocate the array dynamically. 但是实际上,它们是动态分配数组的。

  1. STL lists are linked-lists in C++ (again, as I understand). STL列表是C ++中的链接列表(据我所知)。 Are these doubly-linked lists or singly-linked list? 这些是双链表还是单链表?

Both. 都。 list is doubly linked, and slist is singly linked. list是双向链接,而slist是单个链接。 The name std::forward_list was used in stead of slist when the singly linked list was introduced to the standard library (with minor differences to the STL version). 当将单链接列表引入标准库时,使用名称std::forward_list代替slist (与STL版本略有不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM