Created
February 10, 2019 18:38
-
-
Save adabo/b45db275a4649e9da8161b081e30fa32 to your computer and use it in GitHub Desktop.
How to use std::find() on vector of objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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