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

前端 未结 3 1692
孤独总比滥情好
孤独总比滥情好 2021-02-01 07:30

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::ite         


        
3条回答
  •  北海茫月
    2021-02-01 07:47

    Either

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

    or

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

    or

    inline list::iterator findClosestParticle(list& pl)
    {
        for(list::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. :)

提交回复
热议问题