Skip to content

Commit 6fd6be3

Browse files
committed
replace(_if) : C++26波カッコ初期化に対応 #1311
1 parent 1fe8918 commit 6fd6be3

File tree

4 files changed

+286
-42
lines changed

4 files changed

+286
-42
lines changed

reference/algorithm/ranges_replace.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,34 @@ namespace std::ranges {
1212
class T2,
1313
class Proj = identity>
1414
requires indirectly_writable<I, const T2&> &&
15-
indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T1*>
15+
indirect_binary_predicate<
16+
ranges::equal_to
17+
projected<I, Proj>,
18+
const T1*
19+
>
1620
constexpr I
1721
replace(I first,
1822
S last,
1923
const T1& old_value,
2024
const T2& new_value,
2125
Proj proj = {}); // (1) C++20
26+
template <input_iterator I,
27+
sentinel_for<I> S,
28+
class Proj = identity,
29+
class T1 = projected_value_t<I, Proj>,
30+
class T2 = T1>
31+
requires indirectly_writable<I, const T2&> &&
32+
indirect_binary_predicate<
33+
ranges::equal_to
34+
projected<I, Proj>,
35+
const T1*
36+
>
37+
constexpr I
38+
replace(I first,
39+
S last,
40+
const T1& old_value,
41+
const T2& new_value,
42+
Proj proj = {}); // (1) C++26
2243

2344
template <input_range R,
2445
class T1,
@@ -35,6 +56,21 @@ namespace std::ranges {
3556
const T1& old_value,
3657
const T2& new_value,
3758
Proj proj = {}); // (2) C++20
59+
template <input_range R,
60+
class Proj = identity,
61+
class T1 = projected_value_t<iterator_t<R>, Proj>,
62+
class T2 = T1>
63+
requires indirectly_writable<iterator_t<R>, const T2&> &&
64+
indirect_binary_predicate<
65+
ranges::equal_to,
66+
projected<iterator_t<R>, Proj>,
67+
const T1*
68+
>
69+
constexpr borrowed_iterator_t<R>
70+
replace(R&& r,
71+
const T1& old_value,
72+
const T2& new_value,
73+
Proj proj = {}); // (2) C++26
3874
}
3975
```
4076
* input_iterator[link /reference/iterator/input_iterator.md]
@@ -67,7 +103,17 @@ namespace std::ranges {
67103
正確に `last - first` 回の比較を行う
68104
69105
106+
## 備考
107+
- (1), (2) :
108+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
109+
```cpp
110+
std::vector<T> v;
111+
int n = std::ranges::replace(v, {a, b}, {c, d});
112+
```
113+
114+
70115
## 例
116+
### 基本的な使い方
71117
```cpp example
72118
#include <algorithm>
73119
#include <iostream>
@@ -86,11 +132,50 @@ int main() {
86132
```
87133
* std::ranges::replace[color ff0000]
88134

89-
### 出力
135+
#### 出力
90136
```
91137
3,10,2,10,2,
92138
```
93139

140+
### 波カッコ初期化を入力として使用する (C++26)
141+
```cpp example
142+
#include <algorithm>
143+
#include <iostream>
144+
#include <vector>
145+
146+
struct Point {
147+
int x;
148+
int y;
149+
150+
bool operator==(const Point& other) const = default;
151+
};
152+
153+
int main() {
154+
std::vector<Point> v = {
155+
{1, 2},
156+
{3, 4},
157+
{5, 6},
158+
{1, 2},
159+
};
160+
161+
// 値が{1, 2}の要素をすべて{9, 9}に置き換える
162+
std::ranges::replace(v, {1, 2}, {9, 9});
163+
164+
for (const Point& p : v) {
165+
std::cout << p.x << ',' << p.y << std::endl;
166+
}
167+
}
168+
```
169+
* std::ranges::replace[color ff0000]
170+
171+
#### 出力
172+
```
173+
9,9
174+
3,4
175+
5,6
176+
9,9
177+
```
178+
94179
## バージョン
95180
### 言語
96181
- C++20
@@ -103,3 +188,5 @@ int main() {
103188
104189
## 参照
105190
- [N4861 25 Algorithms library](https://timsong-cpp.github.io/cppwp/n4861/algorithms)
191+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
192+
- C++26で波カッコ初期化 (リスト初期化) に対応した

reference/algorithm/ranges_replace_if.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,5 @@ int main() {
9797

9898
## 参照
9999
- [N4861 25 Algorithms library](https://timsong-cpp.github.io/cppwp/n4861/algorithms)
100+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
101+
- C++26で波カッコ初期化 (リスト初期化) に対応した

reference/algorithm/replace.md

Lines changed: 93 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,54 @@
55

66
```cpp
77
namespace std {
8-
template <class ForwardIterator, class T>
9-
void replace(ForwardIterator first,
10-
ForwardIterator last,
11-
const T& old_value,
12-
const T& new_value); // (1) C++03
13-
14-
template <class ForwardIterator, class T>
15-
constexpr void replace(ForwardIterator first,
16-
ForwardIterator last,
17-
const T& old_value,
18-
const T& new_value); // (1) C++20
19-
20-
template <class ExecutionPolicy, class ForwardIterator, class T>
21-
void replace(ExecutionPolicy&& exec,
22-
ForwardIterator first,
23-
ForwardIterator last,
24-
const T& old_value,
25-
const T& new_value); // (2) C++17
8+
template <class ForwardIterator,
9+
class T>
10+
void
11+
replace(ForwardIterator first,
12+
ForwardIterator last,
13+
const T& old_value,
14+
const T& new_value); // (1) C++03
15+
template <class ForwardIterator,
16+
class T>
17+
constexpr void
18+
replace(ForwardIterator first,
19+
ForwardIterator last,
20+
const T& old_value,
21+
const T& new_value); // (1) C++20
22+
template <class ForwardIterator,
23+
class T = typename iterator_traits<ForwardIterator>::value_type>
24+
constexpr void
25+
replace(ForwardIterator first,
26+
ForwardIterator last,
27+
const T& old_value,
28+
const T& new_value); // (1) C++26
29+
30+
template <class ExecutionPolicy,
31+
class ForwardIterator,
32+
class T>
33+
void
34+
replace(ExecutionPolicy&& exec,
35+
ForwardIterator first,
36+
ForwardIterator last,
37+
const T& old_value,
38+
const T& new_value); // (2) C++17
39+
template <class ExecutionPolicy,
40+
class ForwardIterator,
41+
class T = typename iterator_traits<ForwardIterator>::value_type>
42+
void
43+
replace(ExecutionPolicy&& exec,
44+
ForwardIterator first,
45+
ForwardIterator last,
46+
const T& old_value,
47+
const T& new_value); // (2) C++26
2648
}
2749
```
2850
2951
## 概要
3052
指定された値と一致する要素を指定された値に置き換える。
3153
3254
33-
## 要件
55+
## 適格要件
3456
`*first = new_value` という式が有効でなければならない。
3557
3658
@@ -42,7 +64,17 @@ namespace std {
4264
正確に `last - first` 回の比較を行う
4365
4466
67+
## 備考
68+
- (1), (2) :
69+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
70+
```cpp
71+
std::vector<T> v;
72+
int n = std::replace(v.begin(), v.end(), {a, b}, {c, d});
73+
```
74+
75+
4576
## 例
77+
### 基本的な使い方
4678
```cpp example
4779
#include <algorithm>
4880
#include <iostream>
@@ -60,11 +92,50 @@ int main() {
6092
```
6193
* std::replace[color ff0000]
6294

63-
### 出力
95+
#### 出力
6496
```
6597
3,10,2,10,2,
6698
```
6799

100+
### 波カッコ初期化を入力として使用する (C++26)
101+
```cpp example
102+
#include <algorithm>
103+
#include <iostream>
104+
#include <vector>
105+
106+
struct Point {
107+
int x;
108+
int y;
109+
110+
bool operator==(const Point& other) const = default;
111+
};
112+
113+
int main() {
114+
std::vector<Point> v = {
115+
{1, 2},
116+
{3, 4},
117+
{5, 6},
118+
{1, 2},
119+
};
120+
121+
// 値が{1, 2}の要素をすべて{9, 9}に置き換える
122+
std::replace(v.begin(), v.end(), {1, 2}, {9, 9});
123+
124+
for (const Point& p : v) {
125+
std::cout << p.x << ',' << p.y << std::endl;
126+
}
127+
}
128+
```
129+
* std::replace[color ff0000]
130+
131+
#### 出力
132+
```
133+
9,9
134+
3,4
135+
5,6
136+
9,9
137+
```
138+
68139
69140
## 実装例
70141
```cpp
@@ -80,3 +151,5 @@ void replace(ForwardIterator first, ForwardIterator last,
80151

81152
## 参照
82153
- [P0202R3 Add Constexpr Modifiers to Functions in `<algorithm>` and `<utility>` Headers](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0202r3.html)
154+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
155+
- C++26で波カッコ初期化 (リスト初期化) に対応した

0 commit comments

Comments
 (0)