|
5 | 5 |
|
6 | 6 | ```cpp
|
7 | 7 | namespace std {
|
8 |
| - template<class ForwardIterator, class T> |
| 8 | + template <class ForwardIterator, |
| 9 | + class T> |
9 | 10 | ForwardIterator
|
10 | 11 | lower_bound(ForwardIterator first,
|
11 | 12 | ForwardIterator last,
|
12 | 13 | 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> |
15 | 22 | constexpr ForwardIterator
|
16 | 23 | lower_bound(ForwardIterator first,
|
17 | 24 | ForwardIterator last,
|
18 | 25 | const T& value); // (1) C++20
|
19 | 26 |
|
20 |
| - template<class ForwardIterator, class T, class Compare> |
| 27 | + template <class ForwardIterator, |
| 28 | + class T, |
| 29 | + class Compare> |
21 | 30 | ForwardIterator
|
22 | 31 | lower_bound(ForwardIterator first,
|
23 | 32 | ForwardIterator last,
|
24 | 33 | const T& value,
|
25 | 34 | Compare comp); // (2) C++03
|
26 |
| - |
27 |
| - template<class ForwardIterator, class T, class Compare> |
| 35 | + template <class ForwardIterator, |
| 36 | + class T, |
| 37 | + class Compare> |
28 | 38 | constexpr ForwardIterator
|
29 | 39 | lower_bound(ForwardIterator first,
|
30 | 40 | ForwardIterator last,
|
31 | 41 | const T& value,
|
32 | 42 | 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 |
33 | 51 | }
|
34 | 52 | ```
|
35 | 53 |
|
@@ -71,9 +89,16 @@ namespace std {
|
71 | 89 | 具体的には、[`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); })` とすることで等価の結果が得られる。
|
72 | 90 | - 本関数の要件は、上記の通り C++03 までの方が C++11 よりも厳しい。
|
73 | 91 | しかし、本アルゴリズムの特性上、処理系が 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 | + ``` |
74 | 98 |
|
75 | 99 |
|
76 | 100 | ## 例
|
| 101 | +### 基本的な使い方 |
77 | 102 | ```cpp example
|
78 | 103 | #include <iostream>
|
79 | 104 | #include <algorithm>
|
@@ -152,13 +177,53 @@ int main()
|
152 | 177 | ```
|
153 | 178 | * std::lower_bound[color ff0000]
|
154 | 179 |
|
155 |
| -### 出力 |
| 180 | +#### 出力 |
156 | 181 | ```
|
157 | 182 | 4 pos=2
|
158 | 183 | 4 pos=2
|
159 | 184 | id=4 name=Carol pos=2
|
160 | 185 | ```
|
161 | 186 |
|
| 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 | +
|
162 | 227 |
|
163 | 228 | ## 実装例
|
164 | 229 | ```cpp
|
@@ -201,3 +266,5 @@ lower_bound(ForwardIterator first, ForwardIterator last, const T& value)
|
201 | 266 | - [LWG Issue 384. `equal_range` has unimplementable runtime complexity](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#384)
|
202 | 267 | - [LWG Issue 2150. Unclear specification of `find_end`](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2150)
|
203 | 268 | - [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で波カッコ初期化 (リスト初期化) に対応した |
0 commit comments