Skip to content

Commit 8eae9db

Browse files
committed
lower_bound / upper_bound : C++26波カッコ初期化に対応 #1311
1 parent bd1ef79 commit 8eae9db

File tree

4 files changed

+325
-20
lines changed

4 files changed

+325
-20
lines changed

reference/algorithm/lower_bound.md

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,49 @@
55

66
```cpp
77
namespace std {
8-
template<class ForwardIterator, class T>
8+
template <class ForwardIterator,
9+
class T>
910
ForwardIterator
1011
lower_bound(ForwardIterator first,
1112
ForwardIterator last,
1213
const T& value); // (1) C++03
13-
14-
template<class ForwardIterator, class T>
14+
template <class ForwardIterator,
15+
class T>
16+
constexpr ForwardIterator
17+
lower_bound(ForwardIterator first,
18+
ForwardIterator last,
19+
const T& value); // (1) C++20
20+
template <class ForwardIterator,
21+
class T = typename iterator_traits<ForwardIterator>::value_type>
1522
constexpr ForwardIterator
1623
lower_bound(ForwardIterator first,
1724
ForwardIterator last,
1825
const T& value); // (1) C++20
1926

20-
template<class ForwardIterator, class T, class Compare>
27+
template <class ForwardIterator,
28+
class T,
29+
class Compare>
2130
ForwardIterator
2231
lower_bound(ForwardIterator first,
2332
ForwardIterator last,
2433
const T& value,
2534
Compare comp); // (2) C++03
26-
27-
template<class ForwardIterator, class T, class Compare>
35+
template <class ForwardIterator,
36+
class T,
37+
class Compare>
2838
constexpr ForwardIterator
2939
lower_bound(ForwardIterator first,
3040
ForwardIterator last,
3141
const T& value,
3242
Compare comp); // (2) C++20
43+
template <class ForwardIterator,
44+
class T = typename iterator_traits<ForwardIterator>::value_type,
45+
class Compare>
46+
constexpr ForwardIterator
47+
lower_bound(ForwardIterator first,
48+
ForwardIterator last,
49+
const T& value,
50+
Compare comp); // (2) C++26
3351
}
3452
```
3553
@@ -71,9 +89,16 @@ namespace std {
7189
具体的には、[`partition_point`](partition_point.md)`(first, last, [value](const T& e) { return e < value; })`、あるいは、[`partition_point`](partition_point.md)`(first, last, [value, comp](const T& e) { return comp(e, value); })` とすることで等価の結果が得られる。
7290
- 本関数の要件は、上記の通り C++03 までの方が C++11 よりも厳しい。
7391
しかし、本アルゴリズムの特性上、処理系が C++03 までにしか準拠していない場合でも、昇順に並んでいなくても正常に動作する可能性は高いものと思われる。
92+
- (1), (2) :
93+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
94+
```cpp
95+
std::vector<T> v;
96+
auto it = std::lower_bound(v.begin(), v.end(), {a, b});
97+
```
7498
7599
76100
## 例
101+
### 基本的な使い方
77102
```cpp example
78103
#include <iostream>
79104
#include <algorithm>
@@ -152,13 +177,53 @@ int main()
152177
```
153178
* std::lower_bound[color ff0000]
154179

155-
### 出力
180+
#### 出力
156181
```
157182
4 pos=2
158183
4 pos=2
159184
id=4 name=Carol pos=2
160185
```
161186

187+
### 波カッコ初期化を入力として使用する (C++26)
188+
```cpp example
189+
#include <algorithm>
190+
#include <iostream>
191+
#include <vector>
192+
#include <iterator>
193+
194+
struct Point {
195+
int x;
196+
int y;
197+
198+
bool operator==(const Point& other) const = default;
199+
auto operator<=>(const Point& other) const = default;
200+
};
201+
202+
int main() {
203+
std::vector<Point> v = {
204+
{1, 2},
205+
{1, 2},
206+
{3, 4},
207+
{5, 6},
208+
};
209+
210+
auto it = std::lower_bound(v.begin(), v.end(), {3, 4});
211+
212+
// lower_boundでは指定した値"以上"の値が見つかるので、
213+
// 指定した値を見つけたいなら検索結果の値を比較する必要がある
214+
if (it != v.end() && *it == Point{3, 4}) {
215+
std::size_t pos = std::distance(v.begin(), it);
216+
std::cout << "pos=" << pos << std::endl;
217+
}
218+
}
219+
```
220+
* std::lower_bound[color ff0000]
221+
222+
#### 出力
223+
```
224+
pos=2
225+
```
226+
162227
163228
## 実装例
164229
```cpp
@@ -201,3 +266,5 @@ lower_bound(ForwardIterator first, ForwardIterator last, const T& value)
201266
- [LWG Issue 384. `equal_range` has unimplementable runtime complexity](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#384)
202267
- [LWG Issue 2150. Unclear specification of `find_end`](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2150)
203268
- [P0202R3 Add Constexpr Modifiers to Functions in `<algorithm>` and `<utility>` Headers](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0202r3.html)
269+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
270+
- C++26で波カッコ初期化 (リスト初期化) に対応した

reference/algorithm/ranges_lower_bound.md

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,57 @@ namespace std::ranges {
1010
sentinel_for<I> S,
1111
class T,
1212
class Proj = identity,
13-
indirect_strict_weak_order<const T*, projected<I, Proj>> Comp = ranges::less>
13+
indirect_strict_weak_order<
14+
const T*,
15+
projected<I, Proj>
16+
> Comp = ranges::less>
1417
constexpr I
1518
lower_bound(I first,
1619
S last,
1720
const T& value,
1821
Comp comp = {},
1922
Proj proj = {}); // (1) C++20
23+
template <forward_iterator I,
24+
sentinel_for<I> S,
25+
class Proj = identity,
26+
class T = projected_value_t<I, Proj>,
27+
indirect_strict_weak_order<
28+
const T*,
29+
projected<I, Proj>
30+
> Comp = ranges::less>
31+
constexpr I
32+
lower_bound(I first,
33+
S last,
34+
const T& value,
35+
Comp comp = {},
36+
Proj proj = {}); // (1) C++26
2037

2138
template <forward_range R,
2239
class T,
2340
class Proj = identity,
24-
indirect_strict_weak_order<const T*, projected<iterator_t<R>, Proj>> Comp = ranges::less>
41+
indirect_strict_weak_order<
42+
const T*,
43+
projected<iterator_t<R>,
44+
Proj>
45+
> Comp = ranges::less>
2546
constexpr borrowed_iterator_t<R>
2647
lower_bound(R&& r,
2748
const T& value,
2849
Comp comp = {},
2950
Proj proj = {}); // (2) C++20
51+
template <forward_range R,
52+
class Proj = identity,
53+
class T = projected_value_t<iterator_t<R>, Proj>,
54+
indirect_strict_weak_order<
55+
const T*,
56+
projected<iterator_t<R>,
57+
Proj>
58+
> Comp = ranges::less>
59+
constexpr borrowed_iterator_t<R>
60+
lower_bound(R&& r,
61+
const T& value,
62+
Comp comp = {},
63+
Proj proj = {}); // (2) C++26
3064
}
3165
```
3266
* forward_iterator[link /reference/iterator/forward_iterator.md]
@@ -64,7 +98,17 @@ namespace std::ranges {
6498
## 計算量
6599
最大で log2(`last - first`) + O(1) 回の比較を行う
66100
101+
102+
## 備考
103+
- (1), (2) :
104+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
105+
```cpp
106+
std::vector<T> v;
107+
auto it = std::ranges::lower_bound(v, {a, b});
108+
```
109+
67110
## 例
111+
### 基本的な使い方
68112
```cpp example
69113
#include <iostream>
70114
#include <algorithm>
@@ -139,13 +183,53 @@ int main()
139183
* std::ranges::distance[link /reference/iterator/ranges_distance.md]
140184
* std::ranges::lower_bound[color ff0000]
141185

142-
### 出力
186+
#### 出力
143187
```
144188
4 pos=2
145189
4 pos=2
146190
id=4 name=Bob pos=2
147191
```
148192

193+
### 波カッコ初期化を入力として使用する (C++26)
194+
```cpp example
195+
#include <algorithm>
196+
#include <iostream>
197+
#include <vector>
198+
#include <iterator>
199+
200+
struct Point {
201+
int x;
202+
int y;
203+
204+
bool operator==(const Point& other) const = default;
205+
auto operator<=>(const Point& other) const = default;
206+
};
207+
208+
int main() {
209+
std::vector<Point> v = {
210+
{1, 2},
211+
{1, 2},
212+
{3, 4},
213+
{5, 6},
214+
};
215+
216+
auto it = std::ranges::lower_bound(v, {3, 4});
217+
218+
// lower_boundでは指定した値"以上"の値が見つかるので、
219+
// 指定した値を見つけたいなら検索結果の値を比較する必要がある
220+
if (it != v.end() && *it == Point{3, 4}) {
221+
std::size_t pos = std::distance(v.begin(), it);
222+
std::cout << "pos=" << pos << std::endl;
223+
}
224+
}
225+
```
226+
* std::ranges::lower_bound[color ff0000]
227+
228+
#### 出力
229+
```
230+
pos=2
231+
```
232+
149233
## バージョン
150234
### 言語
151235
- C++20
@@ -158,3 +242,5 @@ id=4 name=Bob pos=2
158242
159243
## 参照
160244
- [N4861 25 Algorithms library](https://timsong-cpp.github.io/cppwp/n4861/algorithms)
245+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
246+
- C++26で波カッコ初期化 (リスト初期化) に対応した

0 commit comments

Comments
 (0)