如何使用迭代器修改对象?使用<list>

Edd*_*223 2 c++ iterator list

所以这是一个例子.恒星的mLocationmSpeed是一个Vector3自定义类型.

我试过了:

Star &star = *iStar;
Star star = *iStar;
Run Code Online (Sandbox Code Playgroud)

iStar->直接使用不适合我的操作员,不知道为什么.那么这样做的正确方法是什么?

   void UniverseManager::ApplySpeedVector()
   { 
   std::list <Star>::const_iterator iStar;

       for (iStar = mStars.begin(); iStar != mStars.end(); ++iStar)
       {
           // how to I get a hold on the object the iterator is pointing to so I can modify its values
                   // i tried  Star &star = *iStar;  this is illegal
                   // tried just using the iStar->mLocation += iStar->mSpeed this also fails due to the operator not accepting the values not sure why
                   // tried other things as well, so what is the proper way to do this?

           iStar->SetLocationData( iStar->mLocation += iStar->mSpeed);
       }
   }
Run Code Online (Sandbox Code Playgroud)

Jam*_*lis 9

std::list<Star>::const_iterator iStar;
Run Code Online (Sandbox Code Playgroud)

您无法通过a修改容器中的对象const_iterator.如果要修改对象,则需要使用iterator(即std::list<Star>::iterator).