我试图找出使用迭代器访问向量中位置的最佳方法。我知道迭代器的行为类似于指针,所以这是我想出的唯一方法。我想知道是否有更好的方式或只是不同的方式。这是代码:
//This is a pointer to a vector of the class Particle BTW. vector < Particle > *particleList;
vector<Particle>::iterator it = particleList->begin();
// I assign a specific position outside the loop to a new iterator that won't be affected
vector<Particle>::iterator it2 = particleList->begin() + 3;
for( it; it != particleList->end(); it++){
it->draw();
//I'm interested in the velocity of this element in particular
cout << it2->vel << endl;
}
谢谢,
米