有没有办法在不同的向量中使用不同类型的迭代器?或者,是否有一个函数将向量中的元素的位置作为整数返回?
std::vector::iterator it; // Iterator // monsterQueue is a vector it = std::find(bot.monsterQueue.begin(), bot.monsterQueue.end(), object); // Check do we have the object in the queue if(it != bot.monsterQueue.end()) // If we do have it { bot.monsterDists.at(it) = mobDist; // monsterDists is vector bot.monsterCoordX.at(it) = PosX; // monsterCoordX is vector bot.monsterCoordY.at(it) = PosY; // monsterCoordY is vector too }
这是一些示例代码,有没有人有任何指针?
index = std::distance( monsterQueue.begin(), it );
简单算一算
it - bot.monsterQueue.begin()
获得索引.
尝试
std::vector::iterator it; // Iterator // monsterQueue is a vector it = std::find(bot.monsterQueue.begin(), bot.monsterQueue.end(), object); // Check do we have the object in the queue if(it != bot.monsterQueue.end()) // If we do have it {
size_t idx = it - bot.monsterQueue.begin()
bot.monsterDists.at(idx) = mobDist; // monsterDists isvector bot.monsterCoordX.at(idx) = PosX; // monsterCoordX is vector bot.monsterCoordY.at(idx) = PosY; // monsterCoordY is vector too }
也可能更好的想法是创建一个包含4个成员'monster',monsterDist和coordinateX以及coordinateY的结构,并将结构对象存储在向量中.