向量,迭代器和std :: find

Ahm*_*med 3 c++ iterator vector

有没有办法在不同的向量中使用不同类型的迭代器?或者,是否有一个函数将向量中的元素的位置作为整数返回?

std::vector<DWORD>::iterator it;        // Iterator

// monsterQueue is a <DWORD> 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 <int> vector
    bot.monsterCoordX.at(it) = PosX; // monsterCoordX is <int> vector
    bot.monsterCoordY.at(it) = PosY; // monsterCoordY is <int> vector too
}
Run Code Online (Sandbox Code Playgroud)

这是一些示例代码,有没有人有任何指针?

Tim*_*mbo 12

index = std::distance( monsterQueue.begin(), it );
Run Code Online (Sandbox Code Playgroud)


Fre*_*man 6

简单算一算

it - bot.monsterQueue.begin()
Run Code Online (Sandbox Code Playgroud)

获得索引.


Nit*_*ide 6

尝试

std::vector<DWORD>::iterator it;        // Iterator

// monsterQueue is a <DWORD> 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
{
Run Code Online (Sandbox Code Playgroud)

size_t idx = it - bot.monsterQueue.begin()

    bot.monsterDists.at(idx) = mobDist; // monsterDists is <int> vector
    bot.monsterCoordX.at(idx) = PosX; // monsterCoordX is <int> vector
    bot.monsterCoordY.at(idx) = PosY; // monsterCoordY is <int> vector too
}
Run Code Online (Sandbox Code Playgroud)

也可能更好的想法是创建一个包含4个成员'monster',monsterDist和coordinateX以及coordinateY的结构,并将结构对象存储在向量中.