C++ 将 std::list<>::iterator 的值获取到指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2754650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:36:00  来源:igfitidea点击:

Getting value of std::list<>::iterator to pointer?

c++stliterator

提问by Onedayitwillmake

How can i loop thru a stl::List and store the value of one of the objects for use later in the function?

我如何通过 stl::List 循环并存储其中一个对象的值以供稍后在函数中使用?

Particle *closestParticle;
for(list<Particle>::iterator p1 = mParticles.begin(); p1 != mParticles.end(); ++p1 )
     {
      // Extra stuff removed
            closestParticle = p1; // fails to compile (edit from comments)
     }

回答by sbi

Either

任何一个

Particle *closestParticle;
for(list<Particle>::iterator it=mParticles.begin(); it!=mParticles.end(); ++it)
    {
      // Extra stuff removed
            closestParticle = &*it;
    }

or

或者

list<Particle>::iterator closestParticle;
for(list<Particle>::iterator it=mParticles.begin(); it!=mParticles.end(); ++it )
    {
      // Extra stuff removed
            closestParticle = it;
    }

or

或者

inline list<Particle>::iterator findClosestParticle(list<Particle>& pl)
{
    for(list<Particle>::iterator it=pl.begin(); it!=pl.end(); ++it )
        {
          // Extra stuff removed
               return it;
        }
    return pl.end();
}

or

或者

template< typename It > 
inline It findClosestParticle(It begin, It end)
{
    while(begin != end )
        {
          // Extra stuff removed
               return begin;
          ++begin;
        }
    return end;
}

These are sorted in increasing personal preference. :)

这些按个人喜好增加排序。 :)

回答by rlbond

For a list, the only way to invalidate an iterator is to eraseit. So I suspect you're calling list.erase(p1)at some point in the loop. You need to make a copy of the iterator, move p1back one, and then erase the copy.

对于 a list,使迭代器无效的唯一方法就是使用erase它。所以我怀疑你list.erase(p1)在循环中的某个时刻打电话。您需要制作迭代器的副本,移p1回一个,然后删除副本。

EDIT: Oh wait, did you mean it doesn't compile? If so, see @sbi's answer. But you really need to word your question in a good way. What is your compile error? Or does it fail at run-time? In this case, however, I believe you mean a compile error.

编辑:哦等等,你的意思是它不能编译吗?如果是这样,请参阅@sbi 的回答。但是你真的需要以一种好的方式来表达你的问题。你的编译错误是什么?或者它在运行时失败?但是,在这种情况下,我相信您的意思是编译错误。

回答by Carl

I'm not an expert on the STL, but I believe the reason it fails to compile is because an iterator is an object that points to another object. In other words, an iterator is a generalization of a pointer. So to do what you'd want with minimal changes to your code, you would first need to de-reference the iterator to get at the value it contains. You'd then use the '&' to get its address and would then assign that address to your pointer variable. This is why ptr=&*it; works.

我不是 STL 的专家,但我相信它无法编译的原因是因为迭代器是一个指向另一个对象的对象。换句话说,迭代器是指针的泛化。因此,要在对代码进行最少更改的情况下执行您想要的操作,您首先需要取消引用迭代器以获取它包含的值。然后您将使用 '&' 获取其地址,然后将该地址分配给您的指针变量。这就是为什么 ptr=&*it; 作品。