Skip to content

Commit

Permalink
flat_map : add value_compare, key_comp() and value_comp() (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
suomesta committed Jan 26, 2025
1 parent c00bf89 commit 1854555
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 3 deletions.
6 changes: 3 additions & 3 deletions reference/flat_map/flat_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ namespace std {
| 名前 | 説明 | 対応バージョン |
|-------------------------------------|----------------------------------------|----------------|
| [`key_comp`](flat_map/key_comp.md.nolink) | キー比較用の関数オブジェクトを取得する | C++23 |
| [`value_comp`](flat_map/value_comp.md.nolink) | 要素比較用の関数オブジェクトを取得する | C++23 |
| [`key_comp`](flat_map/key_comp.md) | キー比較用の関数オブジェクトを取得する | C++23 |
| [`value_comp`](flat_map/value_comp.md) | 要素比較用の関数オブジェクトを取得する | C++23 |
| [`keys`](flat_map/keys.md.nolink) | キーのコンテナを取得する | C++23 |
| [`values`](flat_map/values.md.nolink) | 値のコンテナを取得する | C++23 |
Expand All @@ -101,7 +101,7 @@ namespace std {
| `mapped_type` | 値の型。テンプレートパラメータ `T` | C++23 |
| `value_type` | 要素の型。[`std::pair`](/reference/utility/pair.md)`<key_type, mapped_type>` | C++23 |
| `key_compare` | キー値の大小関係を判定する二項述語の型。テンプレートパラメータ `Compare` | C++23 |
| [`value_compare`](flat_map/value_compare.md.nolink) | 要素値のキー部分で大小関係を判定する二項述語の型。入れ子クラス | C++23 |
| [`value_compare`](flat_map/value_compare.md) | 要素値のキー部分で大小関係を判定する二項述語の型。入れ子クラス | C++23 |
| `reference` | 要素への参照型。[`std::pair`](/reference/utility/pair.md)`<const key_type&, mapped_type&>` | C++23 |
| `const_reference` | 要素への`const`参照型。[`std::pair`](/reference/utility/pair.md)`<const key_type&, const mapped_type&>` | C++23 |
| `size_type` | 要素数を表す符号なし整数型 [`size_t`](/reference/cstddef/size_t.md) | C++23 |
Expand Down
65 changes: 65 additions & 0 deletions reference/flat_map/flat_map/key_comp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# key_comp
* flat_map[meta header]
* std[meta namespace]
* flat_map[meta class]
* function[meta id-type]
* cpp23[meta cpp]

```cpp
key_compare key_comp() const; // C++23
```


## 概要
コンテナに関連づけられたキー比較用の関数オブジェクトを返す。このオブジェクトはコンテナ内の二つの要素のキー値を比較するために利用できる。
この比較オブジェクトはオブジェクトの構築時に与えられ、関数へのポインタでも関数オブジェクトでも良い。いずれの場合でも、これは同じ型の 2 つの引数をとり、[狭義の弱順序](/reference/algorithm.md#strict-weak-ordering)に従って一つ目の引数が二つ目の引数より前のときに `true` を返し、そうでないときに `false` を返す。


## 戻り値
比較オブジェクト。`key_compare` はメンバ型であり、テンプレートパラメータ `Compare` の別名として定義される。


## 計算量
定数時間。


##
```cpp example
#include <flat_map>
#include <iostream>

int main()
{
std::flat_map<int, char> m;
std::flat_map<int, char>::key_compare comp = m.key_comp();

std::cout << comp(1, 2) << std::endl;
std::cout << comp(3, 2) << std::endl;
}
```
* key_comp()[color ff0000]

### 出力
```
1
0
```


## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??


## 関連項目

| 名前 | 説明 |
|-------------------------------------|----------------------------------------------------------|
| [`value_comp`](value_comp.md) | 要素比較用の関数オブジェクトを返す |
| [`value_compare`](value_compare.md) | 要素値のキー部分で大小関係を判定する二項述語の型 |
72 changes: 72 additions & 0 deletions reference/flat_map/flat_map/value_comp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# value_comp
* flat_map[meta header]
* std[meta namespace]
* flat_map[meta class]
* function[meta id-type]
* cpp23[meta cpp]

```cpp
value_compare value_comp() const; // C++23
```


## 概要
コンテナに関連づけられた要素比較用の関数オブジェクトを返す。これはコンテナ内の二つの要素のキー部分を比較するために利用できる。
これは同じ型の 2 つの引数をとり、[狭義の弱順序](/reference/algorithm.md#strict-weak-ordering)に従って一つ目の引数が二つ目の引数の前になる場合に `true`、そうでない場合に `false` を返す。


## 戻り値
要素比較用の関数オブジェクト。
[`value_compare`](value_compare.md) はメンバ型である。`key_compare` とは異なり、単なる型の別名ではなく入れ子クラスである。


## 計算量
定数時間。


##
```cpp example
#include <flat_map>
#include <iostream>
#include <utility>

int main()
{
std::flat_map<int, char> m;
std::flat_map<int, char>::value_compare comp = m.value_comp();

auto p1 = std::make_pair(1, 'a');
auto p2 = std::make_pair(2, 'b');
auto p3 = std::make_pair(3, 'c');

std::cout << comp(p1, p2) << std::endl;
std::cout << comp(p3, p2) << std::endl;
}
```
* value_comp()[color ff0000]
* value_compare[link value_compare.md]

### 出力
```
1
0
```


## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??


## 関連項目

| 名前 | 説明 |
|-------------------------------------|----------------------------------------------------------|
| [`key_comp`](key_comp.md) | キー比較用の関数オブジェクトを取得する |
| [`value_compare`](value_compare.md) | 要素値のキー部分で大小関係を判定する二項述語の型 |
53 changes: 53 additions & 0 deletions reference/flat_map/flat_map/value_compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# value_compare
* flat_map[meta header]
* std[meta namespace]
* flat_map[meta class]
* class[meta id-type]
* cpp23[meta cpp]

```cpp
namespace std {
class flat_map::value_compare;
}
```
## 概要
`value_compare` は `flat_map` の入れ子クラスで、`flat_map::value_type` 型のオブジェクトを比較する関数オブジェクト型である。
比較の基準は `flat_map::key_compare` と同様であるが、`flat_map::key_compare` の関数呼び出し演算子の引数型が `flat_map::key_type` であるのに対して、本クラスの関数呼び出し演算子の比較型は `flat_map::value_type` である点が異なっている。
なお、引数のうち [`flat_map`](../../flat_map.md)`::mapped_type` にあたる [`pair`](../../utility/pair.md) の `second` 部については、比較時には無視される。
## メンバ関数
| 名前 | 説明 | 対応バージョン |
|-----------------------------------------------------------|--------------------|----------------|
| [`operator()`](value_compare/op_call.md.nolink) | 関数呼び出し演算子 | |
一般的な実装では、`key_compare` 型をメンバ変数で保持しており、その変数名を `comp` とすると、以下の動作となる。
```cpp
bool operator()(const_reference x, const_reference y) const {
return comp(x.first, y.first);
}
```
## 例
[`value_comp()`](value_comp.md) の例を参照。
## バージョン
### 言語
- C++23
### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): ??
- [ICC](/implementation.md#icc): ??
- [Visual C++](/implementation.md#visual_cpp): ??
## 関連項目
| 名前 | 説明 |
|-------------------------------------|----------------------------------------------------------|
| [`key_comp`](key_comp.md) | キー比較用の関数オブジェクトを取得する |
| [`value_comp`](value_comp.md) | 要素比較用の関数オブジェクトを返す |

0 comments on commit 1854555

Please sign in to comment.