Jamie Lammas
Jamie Lammas

Reputation: 15

How to find a specific value within vector of objects

I have a vector of class Weapon which then loops through and adding the id values to the vector.

Weapon* weapon;
vector<Weapon*> weaponVector;

for (int i = 0; i < 10; i++)
{
    weapon = new Weapon(i);
    weaponVector.push_back(weapon);
}

I then have a vector iterator to try and find the specified number within the vector.

vector<Weapon*>::iterator findIt;
findIt = find(weaponVector.begin(), weaponVector.end(), 5);

In the Weapon class I have created an operator overload to check if the id's are the same.

bool Weapon::operator==(const Weapon& rhs)
{
    return (rhs.id == id);
}

Quesion:

I am trying to find the number 5 within the weaponVector, however I keep getting this error:

C2446 '==': no conversion from 'const int' to 'Weapon *

Things I have tried:

findIt = find(weaponVector.begin(), weaponVector.end(), Weapon(5));

Weapon five = Weapon(5);
findIt = find(weaponVector.begin(), weaponVector.end(), five);

I keep getting errors regardless what I try. Any help is much appreciated.

Upvotes: 1

Views: 375

Answers (4)

Thomas Matthews
Thomas Matthews

Reputation: 57678

You may want to use copies of weapons in your vector rather than pointers:

std::vector<Weapon> weaponVector;
for (int i = 0; i < 10; i++)
{
    Weapon w = Weapon(i); // Create a temporary weapon.
    weaponVector.push_back(weapon);  // Copy the temporary and place into the vector.
}

By using copies, you eliminate the hassles of dynamic memory management and also having to dereference pointers.

Your search loop:

vector<Weapon>::iterator findIt;
Weapon w5 = Weapon(5);
findIt = find(weaponVector.begin(), weaponVector.end(), w5);

Note that I created a temporary because you don't have an operator==(unsigned int) defined in your class (or you haven't shown it). The find function wants the search value to be of the same type as the objects in the vector.

Upvotes: 0

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

If using C++11, simply use std::find_if with a lambda function:

#include <algorithm>
//...
int number_to_find = 5;
auto findIt = std::find_if(weaponVector.begin(), weaponVector.end(), 
                          [&](Weapon* ptr) { return ptr->id == number_to_find;});

Upvotes: 2

Anedar
Anedar

Reputation: 4265

Your Vector holds pointers to Weapon (Weapon*) while your find is searching for the integer 5 (a const int). Comparing both doesnt make sense, since you dont want to know if any of the Weapon-Objects is stored at memory-address "5".

Comparing a Weapon* to an actual Weapon doesnt make much sense either, since an object and its memory adress are not the same.

So either you use std::find_if or you use a std::vector<Weapon> and store Weapon-Objects directly.

Upvotes: 0

Michael Albers
Michael Albers

Reputation: 3779

Try using the std::find_if function. You can then pass a function which can do the comparison between a Weapon and the id number. See http://en.cppreference.com/w/cpp/algorithm/find.

Upvotes: 1

Related Questions