0
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <algorithm>

using namespace std;

class item {
public:
    string name;
    int damage;
    int heal;
    
};

class enemy {
    int health;
};

int main()
{
    item sword{ "sword", 5, 0 };
    item fist{ "fist", 1, 0 };
    vector <item> inventory;

    string input;

    item weapon = fist;
    item head;
    item chest;
    item legs;
    item feet;

    while (true) {
        cin >> input;

        if (input == "getsword") {
            inventory.push_back(sword);
            cout << "\nSword added to inventory \n";
        }

        if (input == "inventory") {
            for (std::vector<item>::iterator i = inventory.begin(); i != inventory.end(); i++) {
                std::cout << i->name << ", ";
            }
        }

        if (input == "equip") {
            cout << "Which item would you like to equip";
            cin >> input;
            if (input == "sword") {

                if(find(inventory.begin(), inventory.end(), sword) != inventory.end()) {
                    weapon = sword;
                    cout << "sword equipped";
                }
            }
        }
    }
}

Hello, I'm having some trouble trying to find if an object is in a vector. I've tried many solutions with pointers (which I'm having trouble understanding) and I can't seem to get any of them to work.

if(find(inventory.begin(), inventory.end(), sword) != inventory.end())

Any help would be appreciated, thanks.

  • 2
    `item` needs to have an overload for the `==` operator in order to do the comparison. Some suggestions on how best to write that can be found here: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Feb 04 '21 at 22:19
  • 1
    You need to implement `operator==` for `item` to be able to use `std::find`. – R Sahu Feb 04 '21 at 22:19
  • 1
    To use `std::find()`, your `item` class needs to implement `operator==` for comparing 2 `item` objects to each other, eg: `class item { ... bool operator==(const item &rhs) const { return name == rhs.name; } };` Otherwise, use `std::find_if()` instead to search for an element that matches your desired criteria, eg: `auto iter = find_if(inventory.begin(), inventory.end(), [&](const item &elem){ return elem.name == input; }); if (iter != inventory.end()) { weapon = *iter; cout << input << " equipped"; }` – Remy Lebeau Feb 04 '21 at 22:24
  • If you change item to be a struct, then it should do comparison of each of its fields to test equality. If you plan to add methods to item, then implement the == operator. – Ian4264 Feb 04 '21 at 22:26
  • 1
    @Ian4264 Adding methods to a class or struct doesn't affect the need to implement `operator==`, even if it can be auto-generated by the compiler. – Remy Lebeau Feb 04 '21 at 22:27
  • Thank you Remy, this worked perfectly :) – Creamy sadface Feb 04 '21 at 22:59

0 Answers0