获取指向std :: list或std :: forward_list中的节点的指针 [英] Get pointer to node in std::list or std::forward_list

查看:104
本文介绍了获取指向std :: list或std :: forward_list中的节点的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在我的代码中使用std :: list,所以我决定不使用std :: forward_list,因为对于删除(我认为),整个列表将必须遍历,std :::的复杂度为O(N). forward_list(是单个链接列表).但是,当我查看文档时,发现这两个stl容器都具有O(N)复杂度才能删除项目.

I am planning to use std::list in my code, I decided not to use std::forward_list, because for deletions (I figured) the whole list will have to traversed, O(N) complexity for std::forward_list (being a single link list). However, when I looked into the documentation I noticed both the stl containers have O(N) complexity to remove an item.

  • http://www.cplusplus.com/reference/forward_list/forward_list/remove/
  • http://www.cplusplus.com/reference/list/list/remove/

经过一番思考,我找出了原因(我认为).这是因为在两种情况下,都必须扫描整个列表以首先找到该节点,然后再将其删除.是这样吗?

After some thinking I figured out why (I think). It's because in both cases, the whole list has to be scanned to find the node first, and then delete it. Is this right?

然后,我研究了"erase"和"erase_after"方法,它们的复杂度是擦除的元素数(破坏)线性".这是因为,我正在将迭代器传递给节点(有点像指针").但是,我不能(或不想)在我的代码中传递此迭代器来访问节点中的数据.如果列表被修改,我不确定该迭代器是否有效?有想法吗?

I then looked into the "erase" and "erase_after" methods, and their complexity is "Linear in the number of elements erased (destructions).". It's because, I am passing an iterator to the node (which is kind of like a "pointer"). However, I cannot (or prefer not to) pass this iterator around in my code to access the data in the node. I am not sure if this iterator will be valid if the list is modified? Thoughts?

我的问题是,有没有一种方法可以获取指向列表中节点的指针.这样,我知道它在我的程序的整个生命周期内都是有效的,将它传递出去.而且我可以调查一下以访问我的数据.

My question is, is there a way I can get a pointer to the node in the list. That way, I know it will be valid throughout the lifetime of my program, pass it around. And I can just look into it to get access to my data.

推荐答案

但是,我无法(或不想)在我的代码中传递此迭代器来访问节点中的数据.

However, I cannot (or prefer not to) pass this iterator around in my code to access the data in the node.

为什么不呢?迭代器易于使用并且非常轻巧.指针在任何方面都不是更好.

Why not? Iterators are easy to use and are quite lightweight. A pointer isn't better in any way.

我不确定如果修改了列表,此迭代器是否有效?

I am not sure if this iterator will be valid if the list is modified?

对于list,即使修改了列表,任何迭代器也将保持有效.当然,如果 除外,则删除迭代器指向的特定元素.但这很明显,您可以期望有一个不再存在的迭代器(或指针).

For list, any iterator will remain valid, even if the list is modified. Except, of course, if you erase the particular element that is the iterator points to. But that's kind of obvious, you can' expect to have an iterator (or pointer) to something that doesn't exist any more.

(vector更为危险.对向量进行小的更改可能会使所有迭代器失效.)

(vector is more dangerous. One small change to a vector can invalidate all its iterators.)

您可以将指针指向list中的任何单个元素.

You can take a pointer to any individual element in the list.

list<int> iterator it = find(l.begin(), l.end(), 7); // get an iterator
int * ptr = &*it; // get a pointer to the same element.

在许多方面,指针与迭代器相似.但是迭代器功能更强大.迭代器可以增加或减少,以访问列表中的相邻元素.并且可以使用迭代器从列表中删除元素.指针不能做任何事情.

The pointer is similar to the iterator in many respects. But the iterator is a little more powerful. An iterator can be incremented or decremented, to access neighbouring elements in the list. And an iterator can be used to delete an element from the list. A pointer cannot do either of those things.

只要不删除特定元素,迭代器和指针都将保持有效.

Both the iterator and pointer remain valid as long as that particular element isn't removed.

这篇关于获取指向std :: list或std :: forward_list中的节点的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆