###### tags: `C/C++` `STL` # unordered_map/set使用自訂資料結構 在STL容器,`unordered_set`或是`unordered_map`中,如果想要使用自己訂的資料結構當key值,會出現compile error。 這是因為`unordered_map/set`是使用`std::hash`去紀錄key值。 而C++ STL沒有你自己訂的結構的hash。 * 解決方法有兩種: 1. 定義出std::hash 2. 使用boost library ## 使用Boost Library 第二種方法比較簡單。 Boost可當成是STL的擴充。 `boost::hash`幫你把STL的容器都做好了。 ``` #include <boost/functional/hash.hpp> typedef vector<vector<char>> Table; unordered_set<Table, boost::hash<Table>> visited; ``` 這樣就可以了。 另外,直接`#include <boost/unordered_set.hpp>`也可以。 ## References [1] https://www.techiedelight.com/use-std-pair-key-std-unordered_map-cpp/