class template
std::ranges::filter_view(C++20)
概要
- (1): 指定された条件
Pred
を満たす要素だけが要素となるview
- (2):
filter_view
を生成するRangeアダプタオブジェクト
元のRangeから条件を満たす要素を探す処理は遅延評価される。
- 初めてメンバ関数
begin
が呼び出されたときに先頭の要素を決定し、残りはイテレータが進むときに求める。
begin
は償却定数時間で実行できなければならないため、begin
の値はキャッシュされる。
filter_view
の要素を書き換えてもよいが、書き換えた後の要素がPred
を満たさない場合は未定義動作となる。
Rangeコンセプト
borrowed |
sized |
output |
input |
forward |
bidirectional |
random_access |
contiguous |
common |
viewable |
view |
|
|
※ |
○ |
※ |
※ |
|
|
※ |
○ |
○ |
※ V
に従う
効果
- (2): 式
views::filter(E, P)
の効果はfilter_view(E, P)
と等しい
メンバ関数
名前 |
説明 |
対応バージョン |
(constructor) |
コンストラクタ |
C++20 |
base |
V の参照を取得する |
C++20 |
pred |
述語を取得する |
C++20 |
begin |
先頭を指すイテレータを取得する |
C++20 |
end |
番兵を取得する |
C++20 |
継承しているメンバ関数
推論補助
名前 |
説明 |
対応バージョン |
(deduction_guide) |
クラステンプレートの推論補助 |
C++20 |
例
#include <ranges>
#include <iostream>
int main() {
using namespace std;
int a[] = {1, 2, 3, 4, 5};
for (int& i : a | views::filter([](int x){ return x % 2 == 0; })) {
cout << i;
i *= 2; // filterした要素を2倍にする (2倍しても条件を満たすことに注意)
}
cout << '\n';
for (int i : a) {
cout << i;
}
}
出力
24
14385
バージョン
言語
処理系
参照