本文介绍了获取std :: list<> :: iterator的值到指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Particle * closestParticle; 
for(list< Particle> :: iterator p1 = mParticles.begin(); p1!= mParticles.end(); ++ p1)
{
// $ b closestParticle = p1; //无法编译(从注释编辑)
}


解决方案>
 粒子* closestParticle; 
for(list< Particle> :: iterator it = mParticles.begin(); it!= mParticles.end(); ++ it)
{
//删除额外的东西
closestParticle =& * it;
}

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

  inline list< Particle> :: iterator findClosestParticle(list< Particle>& pl)
{
(); it!= pl.end(); ++ it)
{
//除去额外的东西
return it;
}
return pl.end();
}

  template<类型名称> 
inline It findClosestParticle(it begin,It end)
{
while(begin!= end)
{
//删除额外的东西
return begin ;
++ begin;
}
return end;
}

:)


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

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

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. :)

这篇关于获取std :: list&lt;&gt; :: iterator的值到指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:34