From 5415e29dfd912b953b8249e2d1b51dd18f57c4b5 Mon Sep 17 00:00:00 2001 From: yoh Date: Fri, 17 Feb 2023 14:13:27 +0900 Subject: [PATCH] =?UTF-8?q?functional/hash:=20=E9=80=8F=E9=81=8E=E7=9A=84?= =?UTF-8?q?=E3=83=8F=E3=83=83=E3=82=B7=E3=83=A5/=E7=AD=89=E5=80=A4?= =?UTF-8?q?=E6=AF=94=E8=BC=83=E3=81=AE=E4=BE=8B=E7=A4=BA=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=82=92=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C++20標準の`std::equal_to`や`std::hash`は、 透過的な計算動作を示すメンバ型`is_transparent`を持つことはない。 独自の文字列ハッシュ計算クラスと`std::equal_to<>`に置き換える。 --- reference/functional/hash.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/reference/functional/hash.md b/reference/functional/hash.md index 09717118b1..a2df8a8dda 100644 --- a/reference/functional/hash.md +++ b/reference/functional/hash.md @@ -123,16 +123,25 @@ int main() #include #include #include +#include + +struct string_hash { + using is_transparent = void; + // string/string_view/const char*共用ハッシュ計算 + size_t operator()(std::string_view sv) const { + return std::hash{}(sv); + } +}; int main() { - std::unordered_map um = { + std::unordered_map> um = { {"Alice", 3}, {"Bob", 1}, {"Carol", 4} }; - // std::equal_toとstd::hashがis_transparent型を持つ場合、 + // string_hashおよびstd::equal_to<>はいずれもメンバ型にis_transparentを持つため、 // find()などの検索関数に引数を渡す場合に、std::string一時オブジェクトが作られない auto it = um.find("Alice"); if (it != um.end()) {