从指针获取迭代器 [英] Get iterator from pointer
问题描述
以下问题细分错误执行方法我在另一个问题中停止了。
Following question Segmentation fault executing method I stopped in another problem.
给定:
Cluster * base;
Cluster * absorbed;
list<Cluster>::iterator li = clusters.begin();
// li is then pointed to some element.
absorbed = &(*li); // Get a pointer to the chosen element.
base->joinCluster(absorbed); // Perform an action with absorbed.
// li contines the cycle and now point to clusters.end()
// Now, absorbed should be deleted from list. li doesn't point to absorbed anymore.
li = & (* absorbed);
clusters.erase(li);
尝试执行上述相反操作,但g ++返回错误:错误:不匹配'operator ='in'li = absorb'
我该如何做?
Tried to do the reverse of above, but g++ returns an error: error: no match for 'operator=' in 'li = absorbed'
How can I do this?
避免循环遍历列表中的所有元素以再次找到吸收
。
I'm trying to avoid to cycle through all the elements in the list to find again the absorbed
one.
编辑:
很抱歉,我理解我错过了 absorb =&(* li);
li
继续循环,直到 clusters.end()
。
因此,在循环结束时, li
不再指向吸收
。
我想我有两个选项:我要么循环再次通过集群
中的所有元素找到吸收
元件;或者我利用吸收的
指针来从集群
中擦除元素。但是,我如何实现这第二个选项?
Sorry, but i understand I missed to say that after absorbed = &(* li);
li
continues to cycle until clusters.end()
.
So, at the time the cycle ends, li
doesn't point to absorbed
anymore.
I think I have two option: Either I cycle again through all elements in clusters
to find the absorbed
element; Or I take advantage of the absorbed
pointer to erase the element from clusters
. But, how can I achieve this second option?
推荐答案
跳过 li =&(* absorb)
它应该工作很好。
这篇关于从指针获取迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!