From 9d64bb3992e4f626b57dc749cb4fc05e36dad532 Mon Sep 17 00:00:00 2001 From: Akira Takahashi Date: Mon, 15 May 2023 14:25:41 +0900 Subject: [PATCH] =?UTF-8?q?flat=5Fmap=20:=20=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=AE=E3=82=B5=E3=83=B3=E3=83=97?= =?UTF-8?q?=E3=83=AB=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?=20#1078?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GLOBAL_QUALIFY_LIST.txt | 1 + reference/flat_map/flat_map.md | 136 +++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/GLOBAL_QUALIFY_LIST.txt b/GLOBAL_QUALIFY_LIST.txt index 210d6ca9a..da59539dd 100644 --- a/GLOBAL_QUALIFY_LIST.txt +++ b/GLOBAL_QUALIFY_LIST.txt @@ -200,6 +200,7 @@ * t3.join()[link /reference/thread/thread/join.md] * [link /reference/tuple.md] * std::tuple[link /reference/tuple/tuple.md] + * std::tie[link /reference/tuple/tie.md] * [link /reference/type_traits.md] * remove_cvref_t[link /reference/type_traits/remove_cvref.md] * std::false_type[link /reference/type_traits/false_type.md] diff --git a/reference/flat_map/flat_map.md b/reference/flat_map/flat_map.md index ac96b9be4..191a70e6c 100644 --- a/reference/flat_map/flat_map.md +++ b/reference/flat_map/flat_map.md @@ -151,18 +151,154 @@ namespace std { ## 例 ### 基本的な使い方 +```cpp example +#include +#include +#include + +int main() +{ + // stringをキー、intを値として扱う連想配列 + std::flat_map fm = { + {"Carol", 4}, + {"Alice", 3}, + {"Bob", 1} + }; + + // 検索 : キー(string)を指定し、値(int)を得る + int r = fm.at("Alice"); + std::cout << r << std::endl; + + // 全体を出力する + for (const auto& [key, value] : fm) { + std::cout << key << " : " << value << std::endl; + } +} +``` +* fm.at[link flat_map/at.md.nolink] #### 出力 +``` +3 +Alice : 3 +Bob : 1 +Carol : 4 +``` ### ユーザー定義型をキーとして使用する (`operator<=>`を定義) +```cpp example +#include +#include +#include + +// 要素がひとつの場合 +struct MyInt { + int value; + + friend auto operator<=>(const MyInt& a, const MyInt& b) noexcept { + return a.value <=> b.value; + } +}; + +// 要素が複数の場合 +struct Person { + int id; + int age; + std::string name; + + friend auto operator<=>(const Person& a, const Person& b) noexcept { + if (auto comp = a.id <=> b.id; comp != 0) { + return comp; + } + if (auto comp = a.age <=> b.age; comp != 0) { + return comp; + } + return a.name <=> b.name; + } +}; + +int main() +{ + std::flat_map fm1 { + {MyInt{1}, 3}, + {MyInt{2}, 1}, + {MyInt{3}, 4}, + }; + std::cout << fm1[MyInt{2}] << std::endl; + + std::flat_map fm2 { + {Person{1, 18, "Alice"}, 3}, + {Person{2, 30, "Bob"}, 1}, + {Person{3, 30, "Carol"}, 4}, + }; + std::cout << fm2[Person{2, 30, "Bob"}] << std::endl; +} +``` #### 出力 +``` +1 +1 +``` ### ユーザー定義型をキーとして使用する (大小比較の関数オブジェクトを定義) +```cpp example +#include +#include +#include +#include + +// 要素がひとつの場合 +struct MyInt { + int value; +}; + +struct MyIntLess { + bool operator()(const MyInt& a, const MyInt& b) const noexcept { + return a.value < b.value; + } +}; + +// 要素が複数の場合 +struct Person { + int id; + int age; + std::string name; +}; + +struct PersonLess { + bool operator()(const Person& a, const Person& b) const noexcept { + // キーとして比較したい要素を列挙する + return std::tie(a.id, a.age, a.name) < std::tie(b.id, b.age, b.name); + } +}; + +int main() +{ + std::flat_map fm1 { + {MyInt{1}, 3}, + {MyInt{2}, 1}, + {MyInt{3}, 4}, + }; + std::cout << fm1[MyInt{2}] << std::endl; + + std::flat_map fm2 { + {Person{1, 18, "Alice"}, 3}, + {Person{2, 30, "Bob"}, 1}, + {Person{3, 30, "Carol"}, 4}, + }; + std::cout << fm2[Person{2, 30, "Bob"}] << std::endl; +} +``` + #### 出力 +``` +1 +1 +``` ## 参照