Skip to content

Commit f6a62c4

Browse files
committed
algorithm/count : C++26波カッコ初期化に対応 #1311
1 parent 3acf283 commit f6a62c4

File tree

2 files changed

+141
-10
lines changed

2 files changed

+141
-10
lines changed

reference/algorithm/count.md

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,41 @@
55

66
```cpp
77
namespace std {
8-
template <class InputIterator, class T>
8+
template <class InputIterator,
9+
class T>
910
typename iterator_traits<InputIterator>::difference_type
1011
count(InputIterator first,
1112
InputIterator last,
1213
const T& value); // (1) C++03
13-
14-
template <class InputIterator, class T>
14+
template <class InputIterator,
15+
class T>
1516
constexpr typename iterator_traits<InputIterator>::difference_type
1617
count(InputIterator first,
1718
InputIterator last,
1819
const T& value); // (1) C++20
20+
template <class InputIterator,
21+
class T = typename iterator_traits<InputIterator>::value_type>
22+
constexpr typename iterator_traits<InputIterator>::difference_type
23+
count(InputIterator first,
24+
InputIterator last,
25+
const T& value); // (1) C++26
1926

20-
template<class ExecutionPolicy, class ForwardIterator, class T>
27+
template <class ExecutionPolicy,
28+
class ForwardIterator,
29+
class T>
2130
typename iterator_traits<ForwardIterator>::difference_type
2231
count(ExecutionPolicy&& exec,
2332
ForwardIterator first,
2433
ForwardIterator last,
2534
const T& value); // (2) C++17
35+
template <class ExecutionPolicy,
36+
class ForwardIterator,
37+
class T = typename iterator_traits<ForwardIterator>::value_type>
38+
typename iterator_traits<ForwardIterator>::difference_type
39+
count(ExecutionPolicy&& exec,
40+
ForwardIterator first,
41+
ForwardIterator last,
42+
const T& value); // (2) C++16
2643
}
2744
```
2845
* iterator_traits[link /reference/iterator/iterator_traits.md]
@@ -39,7 +56,16 @@ namespace std {
3956
正確に `last - first` 回の比較を行う
4057
4158
59+
## 備考
60+
- (1), (2) :
61+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
62+
```cpp
63+
std::vector<T> v;
64+
int n = std::count(v.begin(), v.begin(), {a, b});
65+
```
66+
4267
## 例
68+
### 基本的な使い方
4369
```cpp example
4470
#include <algorithm>
4571
#include <iostream>
@@ -49,16 +75,50 @@ int main() {
4975
std::vector<int> v = { 1,4,3,3,1,2,2,1 };
5076
5177
// 値が 1 の要素がいくつあるかを数える
52-
std::cout << "count of 1: " << std::count(v.begin(), v.end(), 1) << std::endl;
78+
int n = std::count(v.begin(), v.end(), 1);
79+
std::cout << "count of 1: " << n << std::endl;
5380
}
5481
```
5582
* std::count[color ff0000]
5683

57-
### 出力
84+
#### 出力
5885
```
5986
count of 1: 3
6087
```
6188

89+
### 波カッコ初期化を入力として使用する (C++26)
90+
```cpp example
91+
#include <algorithm>
92+
#include <iostream>
93+
#include <vector>
94+
95+
struct Point {
96+
int x;
97+
int y;
98+
99+
bool operator==(const Point& other) const = default;
100+
};
101+
102+
int main() {
103+
std::vector<Point> v = {
104+
{1, 2},
105+
{3, 4},
106+
{5, 6},
107+
{1, 2},
108+
};
109+
110+
// 値が {1, 2} の要素がいくつあるかを数える
111+
int n = std::count(v.begin(), v.end(), {1, 2});
112+
std::cout << "count of {1,2}: " << n << std::endl;
113+
}
114+
```
115+
* std::count[color ff0000]
116+
117+
#### 出力
118+
```
119+
count of {1,2}: 2
120+
```
121+
62122
63123
## 実装例
64124
```cpp
@@ -85,3 +145,5 @@ typename iterator_traits<InputIterator>::difference_type
85145
## 参照
86146
- [P0202R3 Add Constexpr Modifiers to Functions in `<algorithm>` and `<utility>` Headers](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0202r3.html)
87147
- [P0467R2 Iterator Concerns for Parallel Algorithms](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0467r2.html)
148+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
149+
- C++26で波カッコ初期化 (リスト初期化) に対応した

reference/algorithm/ranges_count.md

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,37 @@ namespace std::ranges {
1212
class Proj = identity>
1313
requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
1414
constexpr iter_difference_t<I>
15-
count(I first, S last, const T& value, Proj proj = {}); // (1) C++20
15+
count(I first,
16+
S last,
17+
const T& value,
18+
Proj proj = {}); // (1) C++20
19+
template <input_iterator I,
20+
sentinel_for<I> S,
21+
class Proj = identity,
22+
class T = projected_value_t<I, Proj>>
23+
requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
24+
constexpr iter_difference_t<I>
25+
count(I first,
26+
S last,
27+
const T& value,
28+
Proj proj = {}); // (1) C++26
1629

1730
template <input_range R,
1831
class T,
1932
class Proj = identity>
2033
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
2134
constexpr range_difference_t<R>
22-
count(R&& r, const T& value, Proj proj = {}); // (2) C++20
35+
count(R&& r,
36+
const T& value,
37+
Proj proj = {}); // (2) C++20
38+
template <input_range R,
39+
class Proj = identity,
40+
class T = projected_value_t<iterator_t<R>, Proj>>
41+
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
42+
constexpr range_difference_t<R>
43+
count(R&& r,
44+
const T& value,
45+
Proj proj = {}); // (2) C++26
2346
}
2447
```
2548
* input_iterator[link /reference/iterator/input_iterator.md]
@@ -55,7 +78,17 @@ namespace std::ranges {
5578
正確に `last - first` 回の比較を行う
5679
5780
81+
## 備考
82+
- (1), (2) :
83+
- C++26 : 引数として波カッコ初期化`{}`を受け付ける
84+
```cpp
85+
std::vector<T> v;
86+
int n = std::ranges::count(v, {a, b});
87+
```
88+
89+
5890
## 例
91+
### 基本的な使い方
5992
```cpp example
6093
#include <algorithm>
6194
#include <iostream>
@@ -65,16 +98,50 @@ int main() {
6598
constexpr std::array v = { 1,4,3,3,1,2,2,1 };
6699
67100
// 値が 1 の要素がいくつあるかを数える
68-
std::cout << "count of 1: " << std::ranges::count(v, 1) << std::endl;
101+
int n = std::ranges::count(v, 1);
102+
std::cout << "count of 1: " << n << std::endl;
69103
}
70104
```
71105
* std::ranges::count[color ff0000]
72106

73-
### 出力
107+
#### 出力
74108
```
75109
count of 1: 3
76110
```
77111

112+
### 波カッコ初期化を入力として使用する (C++26)
113+
```cpp example
114+
#include <algorithm>
115+
#include <iostream>
116+
#include <vector>
117+
118+
struct Point {
119+
int x;
120+
int y;
121+
122+
bool operator==(const Point& other) const = default;
123+
};
124+
125+
int main() {
126+
std::vector<Point> v = {
127+
{1, 2},
128+
{3, 4},
129+
{5, 6},
130+
{1, 2},
131+
};
132+
133+
// 値が {1, 2} の要素がいくつあるかを数える
134+
int n = std::ranges::count(v, {1, 2});
135+
std::cout << "count of {1,2}: " << n << std::endl;
136+
}
137+
```
138+
* std::ranges::count[color ff0000]
139+
140+
#### 出力
141+
```
142+
count of {1,2}: 2
143+
```
144+
78145
79146
## 実装例
80147
```cpp
@@ -124,3 +191,5 @@ inline constexpr count_impl count;
124191

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

0 commit comments

Comments
 (0)