이 튜토리얼에서는 C++에서 정렬되지 않은 사용자 정의 클래스 또는 구조체 집합을 만드는 방법을 이해하는 프로그램에 대해 설명합니다.
이를 위해 구조 유형을 생성한 다음 두 구조 유형을 사용자가 정의한 함수와 비교하여 해시 함수를 저장합니다.
예시
#include <bits/stdc++.h>
using namespace std;
//defined structure
struct Test {
int id;
bool operator==(const Test& t) const{
return (this->id == t.id);
}
};
//defined class for hash function
class MyHashFunction {
public:
size_t operator()(const Test& t) const{
return t.id;
}
};
int main(){
Test t1 = { 110 }, t2 = { 102 }, t3 = { 101 }, t4 = { 115 };
//defining unordered set
unordered_set<Test, MyHashFunction> us;
us.insert(t1);
us.insert(t2);
us.insert(t3);
us.insert(t4);
for (auto e : us) {
cout << e.id << " ";
}
return 0;
} 출력
115 101 110 102