55
66``` cpp
77namespace std {
8- template<class ForwardIterator, class Size, class T>
8+ template <class ForwardIterator,
9+ class Size,
10+ class T>
911 ForwardIterator
1012 search_n(ForwardIterator first,
1113 ForwardIterator last,
1214 Size count,
1315 const T& value); // (1) C++03
14-
15- template<class ForwardIterator, class Size, class T>
16+ template <class ForwardIterator,
17+ class Size,
18+ class T>
1619 constexpr ForwardIterator
1720 search_n(ForwardIterator first,
1821 ForwardIterator last,
1922 Size count,
2023 const T& value); // (1) C++20
24+ template <class ForwardIterator,
25+ class Size,
26+ class T = typename iterator_traits<ForwardIterator >::value_type>
27+ constexpr ForwardIterator
28+ search_n(ForwardIterator first,
29+ ForwardIterator last,
30+ Size count,
31+ const T& value); // (1) C++26
2132
22- template<class ForwardIterator, class Size, class T, class BinaryPredicate>
33+ template <class ForwardIterator,
34+ class Size,
35+ class T,
36+ class BinaryPredicate>
2337 ForwardIterator
2438 search_n(ForwardIterator first,
2539 ForwardIterator last,
2640 Size count,
2741 const T& value,
2842 BinaryPredicate pred); // (2) C++03
29-
30- template<class ForwardIterator, class Size, class T, class BinaryPredicate>
43+ template <class ForwardIterator,
44+ class Size,
45+ class T,
46+ class BinaryPredicate>
3147 constexpr ForwardIterator
3248 search_n(ForwardIterator first,
3349 ForwardIterator last,
3450 Size count,
3551 const T& value,
3652 BinaryPredicate pred); // (2) C++20
53+ template <class ForwardIterator,
54+ class Size,
55+ class T = typename iterator_traits<ForwardIterator >::value_type,
56+ class BinaryPredicate>
57+ constexpr ForwardIterator
58+ search_n(ForwardIterator first,
59+ ForwardIterator last,
60+ Size count,
61+ const T& value,
62+ BinaryPredicate pred); // (2) C++26
3763
38- template <class ExecutionPolicy, class ForwardIterator, class Size, class T>
64+ template <class ExecutionPolicy,
65+ class ForwardIterator,
66+ class Size,
67+ class T>
3968 ForwardIterator
4069 search_n(ExecutionPolicy&& exec,
4170 ForwardIterator first,
4271 ForwardIterator last,
4372 Size count,
4473 const T& value); // (3) C++17
74+ template <class ExecutionPolicy,
75+ class ForwardIterator,
76+ class Size,
77+ class T = typename iterator_traits<ForwardIterator >::value_type>
78+ ForwardIterator
79+ search_n(ExecutionPolicy&& exec,
80+ ForwardIterator first,
81+ ForwardIterator last,
82+ Size count,
83+ const T& value); // (3) C++26
4584
46- template <class ExecutionPolicy, class ForwardIterator, class Size, class T,
85+ template <class ExecutionPolicy,
86+ class ForwardIterator,
87+ class Size,
88+ class T,
4789 class BinaryPredicate>
4890 ForwardIterator
4991 search_n(ExecutionPolicy&& exec,
@@ -52,14 +94,31 @@ namespace std {
5294 Size count,
5395 const T& value,
5496 BinaryPredicate pred); // (4) C++17
97+ template <class ExecutionPolicy,
98+ class ForwardIterator,
99+ class Size,
100+ class T = typename iterator_traits<ForwardIterator >::value_type,
101+ class BinaryPredicate>
102+ ForwardIterator
103+ search_n(ExecutionPolicy&& exec,
104+ ForwardIterator first,
105+ ForwardIterator last,
106+ Size count,
107+ const T& value,
108+ BinaryPredicate pred); // (4) C++26
55109}
56110```
57111
58112## 概要
59113あるシーケンスの中から、特定のサブシーケンスを探す。
60114
115+ - (1): イテレータ範囲`[first, last)`から、値`value`が`count`回連続しているサブシーケンスを検索する
116+ - (2): イテレータ範囲`[first, last)`から、`pred(*it, value) == true`となる要素が`count`回連続しているサブシーケンスを検索する
117+ - (3): 実行ポリシーにしたがって、イテレータ範囲`[first, last)`から、値`value`が`count`回連続しているサブシーケンスを検索する
118+ - (4): 実行ポリシーにしたがって、イテレータ範囲`[first, last)`から、`pred(*it, value) == true`となる要素が`count`回連続しているサブシーケンスを検索する
61119
62- ## 要件
120+
121+ ## 適格要件
63122`Size`は整数型に変換できる型である必要がある。
64123
65124
@@ -73,7 +132,17 @@ namespace std {
73132最大で `last - first` 回の対応する比較もしくは述語が適用される。
74133
75134
135+ ## 備考
136+ - (1), (2), (3), (4) :
137+ - C++26 : パラメータ`value`として波カッコ初期化`{}`を受け付ける
138+ ```cpp
139+ std::vector<T> v;
140+ auto it = std::search_n(v.begin(), v.begin(), 3, {a, b});
141+ ```
142+
143+
76144## 例
145+ ### 基本的な使い方
77146```cpp example
78147#include <algorithm>
79148#include <iostream>
@@ -105,12 +174,48 @@ int main() {
105174* v.cbegin()[ link /reference/vector/vector/cbegin.md]
106175* v.cend()[ link /reference/vector/vector/cend.md]
107176
108- ### 出力
177+ #### 出力
109178```
110179found: index==5
111180found: index==0
112181```
113182
183+ ### 波カッコ初期化を入力として使用する (C++26)
184+ ``` cpp example
185+ #include < algorithm>
186+ #include < iostream>
187+ #include < vector>
188+
189+ struct Point {
190+ int x;
191+ int y;
192+
193+ bool operator==(const Point& other) const = default;
194+ };
195+
196+ int main() {
197+ std::vector<Point > v = {
198+ {1, 2},
199+ {3, 4},
200+ {3, 4},
201+ {5, 6},
202+ };
203+
204+ // 値が {3, 4} が2回連続している場所を探す
205+ auto it = std::search_n(v.begin(), v.end(), 2, {3, 4});
206+ if (it != v.end()) {
207+ int index = std::distance(v.begin(), it);
208+ std::cout << "found: index==" << index << std::endl;
209+ }
210+ }
211+ ```
212+ * std::search_n[color ff0000]
213+
214+ #### 出力
215+ ```
216+ found: index==1
217+ ```
218+
114219
115220## 実装例
116221```cpp
@@ -171,3 +276,5 @@ ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
171276- [ LWG Issue 714. ` search_n ` complexity is too lax] ( http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#714 )
172277- [ LWG Issue 2150. Unclear specification of ` find_end ` ] ( http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2150 )
173278- [ P0202R3 Add Constexpr Modifiers to Functions in ` <algorithm> ` and ` <utility> ` Headers] ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0202r3.html )
279+ - [ P2248R8 Enabling list-initialization for algorithms] ( https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html )
280+ - C++26で波カッコ初期化 (リスト初期化) に対応した
0 commit comments