Ordered
struct node{
int price;
int weight;
node(int p, int w):price(p), weight(w) {}
//Note
//1 define as Member function
//2 const reference parameter
//3 const member function
bool operator< (const node& b) const{
return price == b.price ? weight > b.weight : price < b.price;
}
};
Hashed
struct node{
int id;
string name;
int timeStamp;
node(int i, string d,int t):id(i), name(d),timeStamp(t) {}
bool operator==(const node& other) const{
return id == other.id && name == other.name && timeStamp;
}
};
namespace std {
template<> struct hash<node> {
size_t operator()(node const& n) const{
return hash<int>()(n.id) ^ hash<string>()(n.name);
}
};
}
#include <set>
#include <unordered_set>
#include <string>
class A
{
public:
private:
typedef struct
{
std::string str;
u_int8_t n1;
u_int32_t n2;
}strName;
constexpr static auto hashStruct = [](const strName& structObj) -> const size_t
{
size_t hash;
return hash;
};
constexpr static auto equalStruct = [](const strName& s0, const strName& s1) -> const bool
{
bool b0 = s0.str == s1.str;
bool b1 = s0.n1 == s1.n1;
bool b2 = s0.n2 == s1.n2;
return (b0 && b1 && b2);
};
std::unordered_set<strName, decltype(hashStruct), decltype(equalStruct)> member_i_want_to_declare;
};
int main()
{
A a;
}
#include <cstdint>
#include <iostream>
#include <unordered_set>
#include <string>
class A
{
public:
A()
{
member_i_want_to_declare.insert({ std::string(), 0, 0 });
}
size_t size() const { return member_i_want_to_declare.size(); }
//...
private:
struct strName
{
std::string str;
uint8_t n1;
uint32_t n2;
};
struct HashStruct {
size_t operator()(const strName&) const
{
//boost::crc_32_type result;
//result.process_bytes(structObj.str.data(), structObj.str.size());
//result.process_bytes(&structObj.n1, sizeof(structObj.n1));
//result.process_bytes(&structObj.n2, sizeof(structObj.n2));
size_t hash = 0;//(size_t)result.checksum();
return hash;
}
};
struct EqualStruct {
bool operator()(const strName& s0, const strName& s1) const
{
bool b0 = s0.str == s1.str;
bool b1 = s0.n1 == s1.n1;
bool b2 = s0.n2 == s1.n2;
return (b0 && b1 && b2);
}
};
std::unordered_set<strName, HashStruct, EqualStruct> member_i_want_to_declare;
};
int main()
{
A a;
std::cout << a.size() << '\n';
}