Skip to content

Commit 1350ce7

Browse files
committed
find_last, contains : C++26波カッコ初期化に対応 #1311
1 parent 18c5fd3 commit 1350ce7

File tree

2 files changed

+202
-21
lines changed

2 files changed

+202
-21
lines changed

reference/algorithm/ranges_contains.md

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,59 @@
66

77
```cpp
88
namespace ranges {
9-
// (1)
10-
template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity>
11-
requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
12-
constexpr bool contains(I first, S last, const T& value, Proj proj = {});
13-
14-
// (2)
15-
template<input_range R, class T, class Proj = identity>
16-
requires
17-
indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
18-
constexpr bool contains(R&& r, const T& value, Proj proj = {});
9+
template <input_iterator I,
10+
sentinel_for<I> S,
11+
class T,
12+
class Proj = identity>
13+
requires indirect_binary_predicate<
14+
ranges::equal_to,
15+
projected<I, Proj>,
16+
const T*
17+
>
18+
constexpr bool
19+
contains(I first,
20+
S last,
21+
const T& value,
22+
Proj proj = {}); // (1) C++23
23+
template <input_iterator I,
24+
sentinel_for<I> S,
25+
class Proj = identity,
26+
class T = projected_value_t<I, Proj>>
27+
requires indirect_binary_predicate<
28+
ranges::equal_to,
29+
projected<I, Proj>,
30+
const T*
31+
>
32+
constexpr bool
33+
contains(I first,
34+
S last,
35+
const T& value,
36+
Proj proj = {}); // (1) C++26
37+
38+
template <input_range R,
39+
class T,
40+
class Proj = identity>
41+
requires indirect_binary_predicate<
42+
ranges::equal_to,
43+
projected<iterator_t<R>, Proj>,
44+
const T*
45+
>
46+
constexpr bool
47+
contains(R&& r,
48+
const T& value,
49+
Proj proj = {}); // (2) C++23
50+
template <input_range R,
51+
class Proj = identity,
52+
class T = projected_value_t<iterator_t<R>, Proj>>
53+
requires indirect_binary_predicate<
54+
ranges::equal_to,
55+
projected<iterator_t<R>, Proj>,
56+
const T*
57+
>
58+
constexpr bool
59+
contains(R&& r,
60+
const T& value,
61+
Proj proj = {}); // (2) C++26
1962
}
2063
```
2164
@@ -25,25 +68,72 @@ namespace ranges {
2568
- (1): イテレータ範囲を指定する
2669
- (2): Rangeを直接指定する
2770
71+
2872
## 戻り値
2973
```cpp
3074
ranges::find(std::move(first), last, value, proj) != last
3175
```
3276
* ranges::find[link ranges_find.md]
3377

78+
3479
## 計算量
3580
最大で `last - first` 回比較を行う
3681

3782

83+
## 備考
84+
- (1), (2) :
85+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
86+
```cpp
87+
std::vector<T> v;
88+
bool found = std::ranges::contains(r, {a, b});
89+
```
90+
91+
3892
##
93+
### 基本的な使い方
3994
```cpp example
4095
#include <algorithm>
4196
#include <print>
4297
#include <array>
4398

4499
int main() {
45-
constexpr std::array v = { 3, 1, 4 };
46-
if (std::ranges::contains(v, 1)) {
100+
constexpr std::array ar = { 3, 1, 4 };
101+
if (std::ranges::contains(ar, 1)) {
102+
std::println("found");
103+
} else {
104+
std::println("not found");
105+
}
106+
}
107+
```
108+
* std::ranges::contains[color ff0000]
109+
110+
#### 出力
111+
```
112+
found
113+
```
114+
115+
### 波カッコ初期化を入力として使用する (C++26)
116+
```cpp example
117+
#include <algorithm>
118+
#include <print>
119+
#include <vector>
120+
121+
struct Point {
122+
int x;
123+
int y;
124+
125+
bool operator==(const Point& other) const = default;
126+
};
127+
128+
int main() {
129+
std::vector<Point> v = {
130+
{1, 2},
131+
{3, 4},
132+
{5, 6}
133+
};
134+
135+
bool found = std::ranges::contains(v, {3, 4});
136+
if (found) {
47137
std::println("found");
48138
} else {
49139
std::println("not found");
@@ -52,7 +142,7 @@ int main() {
52142
```
53143
* std::ranges::contains[color ff0000]
54144
55-
### 出力
145+
#### 出力
56146
```
57147
found
58148
```
@@ -90,3 +180,5 @@ inline constexpr contains_impl contains;
90180

91181
## 参照
92182
- [N4950 27 Algorithms library](https://timsong-cpp.github.io/cppwp/n4950/algorithms)
183+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
184+
- C++26で波カッコ初期化 (リスト初期化) に対応した

reference/algorithm/ranges_find_last.md

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,55 @@ namespace std::ranges {
1010
sentinel_for<I> S,
1111
class T,
1212
class Proj = identity>
13-
requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
13+
requires indirect_binary_predicate<
14+
ranges::equal_to,
15+
projected<I, Proj>,
16+
const T*
17+
>
1418
constexpr ranges::subrange<I>
1519
find_last(I first,
1620
S last,
1721
const T& value,
1822
Proj proj = {}); // (1) C++23
23+
template <forward_iterator I,
24+
sentinel_for<I> S,
25+
class Proj = identity,
26+
class T = projected_value_t<I, Proj>>
27+
requires indirect_binary_predicate<
28+
ranges::equal_to,
29+
projected<I, Proj>,
30+
const T*
31+
>
32+
constexpr ranges::subrange<I>
33+
find_last(I first,
34+
S last,
35+
const T& value,
36+
Proj proj = {}); // (1) C++26
1937

2038
template <forward_range R,
2139
class T,
2240
class Proj = identity>
23-
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
41+
requires indirect_binary_predicate<
42+
ranges::equal_to,
43+
projected<iterator_t<R>, Proj>,
44+
const T*
45+
>
2446
constexpr ranges::borrowed_subrange_t<R>
2547
find_last(R&& r,
2648
const T& value,
2749
Proj proj = {}); // (2) C++23
50+
template <forward_range R,
51+
class Proj = identity,
52+
class T = projected_value_t<iterator_t<R>, Proj>>
53+
requires indirect_binary_predicate<
54+
ranges::equal_to,
55+
projected<iterator_t<R>, Proj>,
56+
const T*
57+
>
58+
constexpr ranges::borrowed_subrange_t<R>
59+
find_last(R&& r,
60+
const T& value,
61+
Proj proj = {}); // (2) C++26
2862
}
2963
```
3064
* forward_iterator[link /reference/iterator/forward_iterator.md]
@@ -50,31 +84,82 @@ namespace std::ranges {
5084
最大で `last - first` 回比較を行う
5185
5286
87+
## 備考
88+
- (1), (2) :
89+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
90+
```cpp
91+
std::vector<T> v;
92+
bool found = std::ranges::find_last(r, {a, b});
93+
```
94+
95+
5396
## 例
97+
### 基本的な使い方
5498
```cpp example
5599
#include <algorithm>
56100
#include <array>
57101
#include <iostream>
58102
59103
int main() {
60-
constexpr std::array v = { 3, 1, 4, 1, 5 };
61-
const auto result = std::ranges::find_last(v, 1);
62-
if (result.begin() == v.end()) {
104+
constexpr std::array ar = { 3, 1, 4, 1, 5 };
105+
const std::ranges::subrange result = std::ranges::find_last(ar, 1);
106+
if (result.begin() == ar.end()) {
63107
std::cout << "not found" << std::endl;
64108
} else {
65109
std::cout << "found: " << *result.begin() << std::endl;
66-
std::cout << " pos: " << std::distance(v.begin(), result.begin()) << std::endl;
110+
std::cout << " pos: " << std::distance(ar.begin(), result.begin()) << std::endl;
67111
}
68112
}
69113
```
70114
* std::ranges::find_last[color ff0000]
71115

72-
### 出力
116+
#### 出力
73117
```
74118
found: 1
75119
pos: 3
76120
```
77121

122+
### 波カッコ初期化を入力として使用する (C++26)
123+
```cpp example
124+
#include <algorithm>
125+
#include <array>
126+
#include <iostream>
127+
128+
struct Point {
129+
int x;
130+
int y;
131+
132+
bool operator==(const Point& other) const = default;
133+
};
134+
135+
std::ostream& operator<<(std::ostream& os, const Point& p) {
136+
return os << p.x << ',' << p.y;
137+
}
138+
139+
int main() {
140+
constexpr std::array<Point, 3> ar = {{
141+
{1, 2},
142+
{3, 4},
143+
{5, 6}
144+
}};
145+
146+
const std::ranges::subrange result = std::ranges::find_last(ar, {3, 4});
147+
if (result.begin() == ar.end()) {
148+
std::cout << "not found" << std::endl;
149+
} else {
150+
std::cout << "found: " << *result.begin() << std::endl;
151+
std::cout << " pos: " << std::distance(ar.begin(), result.begin()) << std::endl;
152+
}
153+
}
154+
```
155+
* std::ranges::find_last[color ff0000]
156+
157+
#### 出力
158+
```
159+
found: 3,4
160+
pos: 1
161+
```
162+
78163
79164
## バージョン
80165
### 言語
@@ -87,4 +172,8 @@ found: 1
87172
- [Visual C++](/implementation.md#visual_cpp): ??
88173
89174
## 参照
90-
- [P1223R5 find_last](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1223r5.pdf)
175+
- [P1223R5 `find_last`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1223r5.pdf)
176+
- [P3217R0 Adjoints to "Enabling list-initialization for algorithms": `find_last`](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3217r0.html)
177+
- C++26で波カッコ初期化 (リスト初期化) に対応した
178+
- 関連文書:
179+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)

0 commit comments

Comments
 (0)