Vectors, iterators, and std :: find

Is it possible to use different types of iterators in different vectors? Or is there a function that returns the position of an element in a vector as an integer?

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
}

Something in the sample code, does anyone have pointers?

+3
source share
5 answers

Try

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
{

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
}

It would also probably be better to create a structure with 4 members of the “monster”, monsterDist and X coordinates and Y coordinates and save struct objects in a vector.

+6
source
index = std::distance( monsterQueue.begin(), it );
+12
source

it - bot.monsterQueue.begin()

.

+6

std::vectors:

DWORD find_this = 0x0;
int pos = 0;
for (; i<monsterQueue.size(); ++i)
{
    if (monsterQueue[i]==find_this)
        break;
}

, pos , , .. find_this. , , find_this .

+1
source

Have you ever thought about changing the base monsterQueue type for an object that contains or has links / pointers to monsterDists, etc.

+1
source

Source: https://habr.com/ru/post/1703730/


All Articles