简体   繁体   English

关于C ++中的列表

[英]Regarding Lists in C++

list<int>::iterator it;
  for ( it = l.begin(); it != l.end(); it++ )
    cout << *it << " ";
  cout << endl;

Hi guys, can someone please explain what "lists do not allow random access" means for iterators? 嗨,大家好,有人可以解释一下“列表不允许随机访问”对于迭代器的含义吗? From my understanding, I believe it to mean that lists do not store memory in consecutive memory locations like arrays and vectors so you can't use the [] operator with lists, rather you have to traverse the entire list to find an element? 根据我的理解,我认为这意味着列表不会在数组和向量之类的连续内存位置中存储内存,因此您不能对列表使用[]运算符,而必须遍历整个列表来查找元素? If this is the case, how come you're allowed to increment the it iterator above? 如果是这种情况,为什么允许您增加上面的迭代器?

Thanks so much! 非常感谢!

The statement 该声明

lists do not allow random access 列表不允许随机访问

doesn't mean that you cannot access arbitrary (ie all) elements of a list. 并不意味着您不能访问列表的任意(即所有)元素。 It typically means that you cannot access random elements of a list in the same time ("constant time" or O(1) is another thing you might see in this context). 这通常意味着你不能访问列表的随机因素在同一时间 (“固定时间”或O(1)你可能会在这方面看到另一回事)。 For instance, accessing the second element might be much faster (or slower) than accessing the 1002nd element. 例如,访问第二个元素可能比访问第1002个元素快(或慢)。

The only way to access some arbitrary element is by starting at a well-defined position (eg begin() or rbegin() or some iterator you previously memorized) and then walking backwards or forwards one step at a time. 访问某个任意元素的唯一方法是从一个明确定义的位置begin() (例如begin()rbegin()或您之前存储的某个迭代器),然后一次向前或向后走一步。

Contrast this with eg std::vector which allows you to access an arbitrary element much like with C arrays in "O(1) time" (ie it always takes the same time to access the element, no matter which element it is). 将此与例如std::vector进行对比,它允许您在“ O(1)时间”中访问任意元素,就像使用C数组(即,无论元素是哪个元素,访问元素总是花费相同的时间)。

Lists in C++ are linked lists. C ++中的列表是链接列表。 That means that they don't store data in a consecutive memory block like an array of a vector does; 这意味着它们不会像向量数组那样将数据存储在连续的内存块中。 instead, they consist of a number of nodes, each of whose has a link to the next and the previous node. 相反,它们由多个节点组成,每个节点都有到下一个和上一个节点的链接。 The list itself only keeps links to the first and the last node. 列表本身仅保留指向第一个和最后一个节点的链接。 This allows inserting an element in the middle without computationally expensive reallocating and copying of other elements. 这允许在中间插入一个元素,而无需在计算上重新分配和复制其他元素。

When you increment the iterator, you are basically just following the link to the next node of the list. 当增加迭代器时,基本上就是在链接到列表的下一个节点。

Because an iterator is not a pointer -- it just looks like one. 因为迭代器不是指针,所以它看起来像一个指针。

When you say ++it , that's merely a shorthand for it.operator++() . 当您说++it ,它只是it.operator++()的简写。 Inside that method, you can do anything you want. 在该方法内,您可以执行任何所需的操作。 You are not required to merely add one to a pointer. 您不需要仅将一个添加到指针。

In the case of a list::iterator, it takes on the address of the next node in the list. 对于list :: iterator,它采用列表中下一个节点的地址。

Well, somehow you´ve answered it yourself. 好吧,您已经以某种方式自己回答了。

In a list, you can´t "jump" to random elements with [], all you can do 在列表中,您无法使用[]将“元素”“跳转”到随机元素,
is to get the next one (compared to the current one where you are). 是获得下一个(与当前所在位置相比)。

Starting at the first element and do something for each element until the end is no problem. 从第一个元素开始,对每个元素进行操作,直到结束为止都没有问题。
Do something for the first element, get the next one, do something, get the next one... 为第一个元素做某事,获取下一个,做某事,获取下一个...

Each node contains address of the next node. 每个节点包含下一个节点的地址。 By iterating you jump to the next node automatically. 通过迭代,您将自动跳至下一个节点。

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

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