From d91c1da0f13281d896a93c82e69da2b0f8eb611b Mon Sep 17 00:00:00 2001 From: Akira Takahashi Date: Thu, 25 May 2023 13:29:01 +0900 Subject: [PATCH] =?UTF-8?q?flat=5Fmap=20:=20contains=E3=81=A8count?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=20#1078?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reference/flat_map/flat_map.md | 6 +- reference/flat_map/flat_map/contains.md | 74 ++++++++++++++++++ reference/flat_map/flat_map/count.md | 99 +++++++++++++++++++++++++ reference/flat_map/flat_map/find.md | 2 +- reference/map/map/contains.md | 3 + reference/map/map/count.md | 3 +- reference/map/multimap/contains.md | 3 + reference/set/multiset/contains.md | 3 + reference/set/multiset/count.md | 1 + reference/set/set/contains.md | 3 + 10 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 reference/flat_map/flat_map/contains.md create mode 100644 reference/flat_map/flat_map/count.md diff --git a/reference/flat_map/flat_map.md b/reference/flat_map/flat_map.md index e75b17b669..2298004e7b 100644 --- a/reference/flat_map/flat_map.md +++ b/reference/flat_map/flat_map.md @@ -76,9 +76,9 @@ namespace std { |---------------------------------------|--------------------------------------------|-------| | [`operator[]`](flat_map/op_at.md) | 指定したキーを持つ要素を取得する | C++23 | | [`at`](flat_map/at.md) | 指定したキーを持つ要素を取得する | C++23 | -| [`count`](flat_map/count.md.nolink) | 指定したキーにマッチする要素の数を取得する | C++23 | +| [`count`](flat_map/count.md) | 指定したキーにマッチする要素の数を取得する | C++23 | | [`find`](flat_map/find.md) | 指定したキーで要素を探す | C++23 | -| [`contains`](flat_map/contains.md.nolink) | 指定したキーの要素が含まれているかを判定する | C++23 | +| [`contains`](flat_map/contains.md) | 指定したキーの要素が含まれているかを判定する | C++23 | | [`equal_range`](flat_map/equal_range.md.nolink) | 指定したキーにマッチする要素範囲を取得する | C++23 | | [`lower_bound`](flat_map/lower_bound.md.nolink) | 与えられた値より小さくない最初の要素へのイテレータを取得する | C++23 | | [`upper_bound`](flat_map/upper_bound.md.nolink) | 特定の値よりも大きい最初の要素へのイテレータを取得する | C++23 | @@ -89,6 +89,8 @@ namespace std { |-------------------------------------|----------------------------------------|----------------| | [`key_comp`](flat_map/key_comp.md.nolink) | キー比較用の関数オブジェクトを取得する | C++23 | | [`value_comp`](flat_map/value_comp.md.nolink) | 要素比較用の関数オブジェクトを取得する | C++23 | +| [`keys`](flat_map/keys.md.nolink) | キーのコンテナを取得する | C++23 | +| [`values`](flat_map/values.md.nolink) | 値のコンテナを取得する | C++23 | ## メンバ型 diff --git a/reference/flat_map/flat_map/contains.md b/reference/flat_map/flat_map/contains.md new file mode 100644 index 0000000000..4f8e68a1ca --- /dev/null +++ b/reference/flat_map/flat_map/contains.md @@ -0,0 +1,74 @@ +# contains +* flat_map[meta header] +* std[meta namespace] +* flat_map[meta class] +* function[meta id-type] +* cpp23[meta cpp] + +```cpp +bool contains(const key_type& x) const; // (1) C++23 + +template +bool contains(const K& x) const; // (2) C++23 +``` + + +## 概要 +指定されたキー`x`に一致する要素がコンテナに含まれているかを判定する。 + +- (1) : クラスのテンプレートパラメータ`key_type`型のキーを受け取る +- (2) : `key_type`と比較可能な`K`型のキーを受け取る + + +## 戻り値 +以下と等価: + +```cpp +return find(x) != end(); +``` +* find[link find.md] +* end()[link end.md.nolink] + + +## 計算量 +対数時間 + + +## 例 +```cpp example +#include +#include + +int main() +{ + std::flat_map m = { + {'a', 3}, + {'b', 1}, + {'c', 4} + }; + + // キー'b'の要素が含まれているか + if (m.contains('b')) { + std::cout << "contain" << std::endl; + } + else { + std::cout << "doesn't contain" << std::endl; + } +} +``` +* contains[color ff0000] + +### 出力 +``` +contain +``` + + +## バージョン +### 言語 +- C++23 + +### 処理系 +- [Clang](/implementation.md#clang): ?? +- [GCC](/implementation.md#gcc): ?? +- [Visual C++](/implementation.md#visual_cpp): ?? diff --git a/reference/flat_map/flat_map/count.md b/reference/flat_map/flat_map/count.md new file mode 100644 index 0000000000..b1fbb97af5 --- /dev/null +++ b/reference/flat_map/flat_map/count.md @@ -0,0 +1,99 @@ +# count +* flat_map[meta header] +* std[meta namespace] +* flat_map[meta class] +* function[meta id-type] +* cpp23[meta cpp] + +```cpp +size_type count(const key_type& x) const; // (1) C++23 + +template +size_type count(const K& x) const; // (2) C++23 +``` + +## 概要 +キー `x` を検索し、コンテナ内に見つかった要素の数を返す。`flat_map` コンテナはキーの重複を許さないため、この関数は実際には要素が見つかったときに 1 を、そうでないときに 0 を返す。 + +- (1) : クラスのテンプレートパラメータ`key_type`型のキーを受け取る +- (2) : `key_type`と比較可能な`K`型のキーを受け取る + + +## 戻り値 +- (1) : `x`と等価なキーの要素が見つかった場合は1、そうでない場合は0を返す。 +- (2) : `key_compare`型の関数オブジェクトを`c`、コンテナ内の各要素が持つキーを`k`として、キーが等価か判定する式`!c(k, x) && !c(x, k)`が`true`となる要素が見つかった場合は1、そうでない場合は0を返す。 + + +## 計算量 +``` +log(b.size()) + b.count(k) +``` +* b.size()[link size.md] + + +## 備考 +- (2) : この関数がオーバーロード解決に参加する条件は、[`find()`](find.md)メンバ関数の備考欄を参照 +- [`std::flat_multimap`](/reference/flat_map/flat_multimap.md.nolink)クラスとの共通インタフェースを使用する必要がなければ、この関数の代わりに[`contains()`](contains.md)メンバ関数を使用することを推奨する + + +## 例 +```cpp example +#include +#include +#include + +int main() +{ + // (1) + { + std::flat_map fm = { + {"Alice", 3}, + {"Bob", 1}, + {"Carol", 4} + }; + + std::size_t n = fm.count("Bob"); + if (n > 0) { // 見つかった + std::cout << "found" << std::endl; + } + } + + // (2) + { + std::flat_map> fm = { + {"Alice", 3}, + {"Bob", 1}, + {"Carol", 4} + }; + + // std::lessのvoidに対する特殊化を使用することで、 + // 文字列リテラルをcount()関数の引数として渡した際に、 + // std::string型の一時オブジェクトが生成されない。 + std::size_t n = fm.count("Bob"); + if (n > 0) { // 見つかった + std::cout << "found" << std::endl; + } + } +} +``` +* fm.count[color ff0000] +* std::less[link /reference/functional/less.md] + +### 出力 +``` +found +found +``` + +## バージョン +### 言語 +- C++23 + +### 処理系 +- [Clang](/implementation.md#clang): ?? +- [GCC](/implementation.md#gcc): ?? +- [Visual C++](/implementation.md#visual_cpp): ?? + + +## 関連項目 +- [`contains()`](contains.md) diff --git a/reference/flat_map/flat_map/find.md b/reference/flat_map/flat_map/find.md index 6541baebbe..99f5e642f7 100644 --- a/reference/flat_map/flat_map/find.md +++ b/reference/flat_map/flat_map/find.md @@ -104,5 +104,5 @@ int main() ## 関連項目 -- [`contains()`](contains.md.nolink) +- [`contains()`](contains.md) diff --git a/reference/map/map/contains.md b/reference/map/map/contains.md index a102feb735..6b35fbba73 100644 --- a/reference/map/map/contains.md +++ b/reference/map/map/contains.md @@ -16,6 +16,9 @@ bool contains(const K& x) const; // (2) ## 概要 指定されたキー`x`に一致する要素がコンテナに含まれているかを判定する。 +- (1) : クラスのテンプレートパラメータ`key_type`型のキーを受け取る +- (2) : `key_type`と比較可能な`K`型のキーを受け取る + ## 戻り値 以下と等価: diff --git a/reference/map/map/count.md b/reference/map/map/count.md index ad73fabefd..c8664399a9 100644 --- a/reference/map/map/count.md +++ b/reference/map/map/count.md @@ -31,7 +31,8 @@ log(b.size()) + b.count(k) ## 備考 -- (2) : この関数がオーバーロード解決に参加する条件は、[`find()`](find.md)メンバ関数の備考欄を参照。 +- (2) : この関数がオーバーロード解決に参加する条件は、[`find()`](find.md)メンバ関数の備考欄を参照 +- [`std::multimap`](/reference/map/multimap.md)クラスとの共通インタフェースを使用する必要がなければ、この関数の代わりに[`contains()`](contains.md)メンバ関数を使用することを推奨する ## 例 diff --git a/reference/map/multimap/contains.md b/reference/map/multimap/contains.md index 63c7c74956..4a902da65a 100644 --- a/reference/map/multimap/contains.md +++ b/reference/map/multimap/contains.md @@ -16,6 +16,9 @@ bool contains(const K& x) const; // (2) ## 概要 指定されたキー`x`に一致する要素がコンテナに含まれているかを判定する。 +- (1) : クラスのテンプレートパラメータ`key_type`型のキーを受け取る +- (2) : `key_type`と比較可能な`K`型のキーを受け取る + ## 戻り値 以下と等価: diff --git a/reference/set/multiset/contains.md b/reference/set/multiset/contains.md index 80612edcd5..10f93aa92d 100644 --- a/reference/set/multiset/contains.md +++ b/reference/set/multiset/contains.md @@ -16,6 +16,9 @@ bool contains(const K& x) const; // (2) ## 概要 指定されたキー`x`に一致する要素がコンテナに含まれているかを判定する。 +- (1) : クラスのテンプレートパラメータ`key_type`型のキーを受け取る +- (2) : `key_type`と比較可能な`K`型のキーを受け取る + ## 戻り値 以下と等価: diff --git a/reference/set/multiset/count.md b/reference/set/multiset/count.md index ae5ea12d13..8f801ed4b4 100644 --- a/reference/set/multiset/count.md +++ b/reference/set/multiset/count.md @@ -32,6 +32,7 @@ log(b.size()) + b.count(k) ## 備考 - (2) : この関数がオーバーロード解決に参加する条件は、[`find()`](find.md)メンバ関数の備考欄を参照。 +- [`std::multiset`](/reference/set/multiset.md)クラスとの共通インタフェースを使用する必要がなければ、この関数の代わりに[`contains()`](contains.md)メンバ関数を使用することを推奨する ## 例 diff --git a/reference/set/set/contains.md b/reference/set/set/contains.md index 6100e511ee..fc3f07756e 100644 --- a/reference/set/set/contains.md +++ b/reference/set/set/contains.md @@ -16,6 +16,9 @@ bool contains(const K& x) const; // (2) ## 概要 指定されたキー`x`に一致する要素がコンテナに含まれているかを判定する。 +- (1) : クラスのテンプレートパラメータ`key_type`型のキーを受け取る +- (2) : `key_type`と比較可能な`K`型のキーを受け取る + ## 戻り値 以下と等価: