简体   繁体   English

如何在C ++中将元素存储在STL列表中

[英]how elements are stored in list of STL in C++

I was reading about lists in Standard Template library in C++. 我正在阅读有关C ++标准模板库中的列表的信息。 I read elements can not be accessed using index. 我读取了无法使用索引访问的元素。 Can any one please let me know how the lists are stored in memory? 谁能让我知道列表如何存储在内存中? Is it sequential? 它是顺序的吗? I know how linked lists are implemented. 我知道链接列表是如何实现的。 Are the lists in STL are also implemented in same fashion? STL中的列表是否也以相同的方式实现? ie a pointer will be having address of next element? 即一个指针将具有下一个元素的地址?

If that is the case, how increment of iterator is able to point to the next element in the list? 如果是这样,迭代器的增量如何能够指向列表中的下一个元素? Is the increment operator on iterator is overloaded? 迭代器上的增量运算符是否过载?

std::list<> is a sequence container, as are std::vector<> , std::deque . std::list<>是序列容器, std::vector<> std::deque How they are implemented is implementation-dependent. 它们的实现方式取决于实现方式。 But their behavior and required characteristics are defined by the standard. 但是它们的行为和所需的特性由标准定义。

Lists, for example, must have constant-time insertion. 例如,列表必须具有固定时间插入。 A vector does not require constant time insertion but does have other requirements (such as const-time random-access). 一种载体, 不需要恒定的时间插入具有其它要求(如常量时间随机访问)。 Such requirements gravitate implementations to common algorithms ( std::list is commonly a double-linked-list in the traditional-sense, for example). 这样的要求使实现倾向于通用算法(例如, std::list list在传统意义上通常是一个双链表)。

Iterators "work" on containers such as std::list<> by attaching container-state to the iterator. 通过将容器状态附加到迭代器,迭代器可以在诸如std::list<>的容器上“工作”。 A list iterator, for example, could know its iterating container and its "place" in the sequence enumeration via a pointer to the current node in the underlying implementation. 例如,列表迭代器可以通过指向底层实现中当前节点的指针来知道其迭代容器及其在序列枚举中的“位置”。 Advancing the iterator simply means having it move through the internal node pointer to that pointer's "next". 推进迭代器只是意味着让它在内部节点指针中移动到该指针的“下一个”。

Don't try to attach too much meaning to the implementation underneath the mandated behavior. 不要在强制性行为下的实现中赋予过多含义。 So long as the behavioral requirements are met, the underlying implementation could quite-literally be anything . 只要满足了行为要求,从根本上说,底层实现就可以是任何东西

std::list is usually implemented as a linked list, each node storing a list element and pointers to previous and next nodes. std::list通常实现为链接列表,每个节点都存储一个列表元素以及指向上一个和下一个节点的指针。 It is common to implement linked lists with bounding fake nodes in the very beginning of the list and at the end of it (it makes the implementation a bit simpler and more beautiful). 通常在链表的开头和结尾都使用带有虚假节点的链表来实现(这使实现更加简单和美观)。 In gcc implementation they use only one node both for the starting and ending fake nodes, so the list is actually cyclic. 在gcc实现中,它们仅使用一个节点作为起始和结束伪节点,因此该列表实际上是循环的。 This fake node does not contain any list element (if it contained, it would be a problem by many reasons). 该伪节点不包含任何列表元素(如果包含该列表元素,则由于许多原因会造成问题)。 This node has a non-template type (lets say basic_node ) which looks like this: 该节点具有非模板类型(让我们说basic_node ),如下所示:

struct basic_node
{
    basic_node * prev, * next;
};

Other, value-containing, nodes are templated: 其他包含值的节点被模板化:

template <typename T>
struct node : basic_node
{
    T value;
};

std::list::iterator stores a pointer to basic_node . std::list::iterator存储指向basic_node的指针。 This gives the following advantage: most of the iterator's code is independent of the template parameter. 这具有以下优点:迭代器的大多数代码独立于template参数。 For example, to increment the iterator, we can just do something like node_ptr = node_ptr->next; 例如,要增加迭代器,我们可以做类似node_ptr = node_ptr->next; , where node_ptr has type basic_node . ,其中node_ptr类型为basic_node

When dereferencing, std::list<T>::iterator can cast the pointer to node to appropriate templated node: return static_cast<node<T> *>(node_ptr)->value; 取消引用时, std::list<T>::iterator可以将指向节点的指针return static_cast<node<T> *>(node_ptr)->value;为适当的模板化节点: return static_cast<node<T> *>(node_ptr)->value; .

List is not stored sequentially. 列表没有顺序存储。 You're looking for std::vector if that's what you want. 如果您要的是您正在寻找std::vector

From documentation , "Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions. List containers are implemented as doubly-linked lists" 文档中 ,“列表是序列容器,允许在序列中的任何位置进行恒定时间的插入和擦除操作,并且可以双向迭代。列表容器被实现为双向链接的列表”

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

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