unordered_set::find
#include <unordered_set>
#include <iostream>
#include <algorithm>
#include <functional>
using std::unordered_set,
std::hash,
std::size_t,
std::cerr;
class Temp{
public:
int a, b;
Temp(int i, int j):a(i), b(j){}
};
bool operator==(const Temp& l, const Temp& r)
{
return l.a == r.a && l.b == r.b;
}
template<>
struct std::hash<Temp>
{
size_t operator()(const Temp& temp) const noexcept
{
size_t h1 = hash<int>{}(temp.a);
size_t h2 = hash<int>{}(temp.b);
return h1 ^ (h2 << 1); // or use boost::hash_combine
}
};
typedef unordered_set<Temp> my_unordered_set;
int main(){
my_unordered_set a = {Temp(1,2), Temp(3,5)};
my_unordered_set::iterator i = a.find(Temp(3,5));
cerr << "i->a:" << i->a << " i->b: " << i->b << "\n";
return 0;
}
C++ Examples© 2024 TBD