liliumdev
liliumdev

Reputation: 1179

Vectors, structs and std::find

Again me with vectors. I hope I'm not too annoying. I have a struct like this :

struct monster 
{
    DWORD id;
    int x;
    int y;
    int distance;
    int HP;
};

So I created a vector :

std::vector<monster> monsters;

But now I don't know how to search through the vector. I want to find an ID of the monster inside the vector.

DWORD monster = 0xFFFAAA;
it = std::find(bot.monsters.begin(), bot.monsters.end(), currentMonster);

But obviously it doesn't work. I want to iterate only through the .id element of the struct, and I don't know how to do that. Help is greatly appreciated. Thanks !

Upvotes: 43

Views: 84800

Answers (7)

Aditya Sharma
Aditya Sharma

Reputation: 65

You can write a function as below:

monster* findMonster(DWORD currentMonster) {
    for (auto it = bot.monsters.begin(); it != bot.monsters.end(); it++) {
        if (it->id == currentMonster) {
            return &(*it);
        }
    }
    return NULL;
}

It returns a pointer to the stored node if it's found in the vector, otherwise returns NULL.

Please note that return it; won't work directly.

Upvotes: 0

Semjon M&#246;ssinger
Semjon M&#246;ssinger

Reputation: 1900

This is a complete sample based on the answer of Johannes Schaub (boost version).

#include <algorithm>
#include <boost/bind.hpp>

struct monster 
{
    DWORD id;
    int x;
    int y;
    int distance;
    int HP;
};

int main ()
{
    std::vector<monster> monsters;

    monster newMonster;
    newMonster.id    = 1;
    newMonster.x     = 10;
    monsters.push_back ( newMonster );

    newMonster.id    = 2;
    newMonster.x     = 20;
    monsters.push_back ( newMonster );

    newMonster.id    = 2;
    newMonster.x     = 30;
    monsters.push_back ( newMonster );

    DWORD monsterId = 2;

    std::vector< monster >::iterator it = std::find_if ( monsters.begin (), monsters.end (), 
        boost::bind ( &monster::id, _1 ) == monsterId );

    return 0;
}

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507423

std::find_if:

it = std::find_if(bot.monsters.begin(), bot.monsters.end(), 
        boost::bind(&monster::id, _1) == currentMonster);

Or write your own function object if you don't have boost. Would look like this

struct find_id : std::unary_function<monster, bool> {
    DWORD id;
    find_id(DWORD id):id(id) { }
    bool operator()(monster const& m) const {
        return m.id == id;
    }
};

it = std::find_if(bot.monsters.begin(), bot.monsters.end(), 
         find_id(currentMonster));

Upvotes: 42

Evgeny Lazin
Evgeny Lazin

Reputation: 9423

You need to write your own search predicate:

struct find_monster
{
    DWORD id;
    find_monster(DWORD id) : id(id) {}
    bool operator () ( const monster& m ) const
    {
        return m.id == id;
    }
};

it = std::find_if( monsters.begin(), monsters.end(), find_monster(monsterID));

Upvotes: 21

pm100
pm100

Reputation: 50220

or put the monsters in a map instead of a vector

or if they must be in a vector create an index map ie map of ID to vector index

Upvotes: 1

Matthieu N.
Matthieu N.

Reputation:

how about:

std::find_if(monsters.begin(), 
             monsters.end(), 
             [&cm = currentMonster]
             (const monster& m) -> bool { return cm == m; }); 

Upvotes: 28

dirkgently
dirkgently

Reputation: 111336

Take a look at the std::find template, the third parameter especially:

template<class InputIterator, class EqualityComparable>
InputIterator find(InputIterator first, InputIterator last,
               const EqualityComparable& value);

What is this EqualityComparable? Again from the documentation:

A type is EqualityComparable if objects of that type can be 
compared for equality using operator==, and if operator== is 
an equivalence relation. 

Now, your type monster needs to define such an operator. If you don't the compiler generates one for you (as also the default ctor and the dtor) which does a memcmp sort of thing which doesn't work in your case. So, to use std::find first define a comparator function/functor that the algorithm can use to match your currentMonster i.e. something along the lines of:

 struct monster {
  // members
  bool operator==(const monster& l, const monster& r) const
  {
     return l.id == r.id;
  }
 };

Upvotes: 9

Related Questions