Skip to content

Instantly share code, notes, and snippets.

@adabo
Created February 10, 2019 18:38
Show Gist options
  • Save adabo/b45db275a4649e9da8161b081e30fa32 to your computer and use it in GitHub Desktop.
Save adabo/b45db275a4649e9da8161b081e30fa32 to your computer and use it in GitHub Desktop.
How to use std::find() on vector of objects
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
enum ItemType
{
TORCH,
KEY,
GEM,
PEBBLE
};
class Item
{
public:
bool operator==(Item const& other) const {
return this == other;
}
Item(ItemType)
: type(type)
{
}
ItemType type;
};
int main()
{
std::vector<Item> backpack;
backpack.push_back(TORCH);
backpack.push_back(KEY);
backpack.push_back(GEM);
auto result = std::find(backpack.begin(), backpack.end(), TORCH);
if (result != backpack.end())
std::cout << "Found.\n";
else
std::cout << "Not found.\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment