- ranges[meta header]
- std::ranges[meta namespace]
- class template[meta id-type]
- cpp20[meta cpp]
namespace std::ranges {
template<input_range V, indirect_unary_predicate<iterator_t<V>> Pred>
requires view<V> && is_object_v<Pred>
class filter_view : public view_interface<filter_view<V, Pred>> { …… }; // (1)
namespace views {
inline constexpr /*unspecified*/ filter = /*unspecified*/; // (2)
}
}
- (1): 指定された条件
Pred
を満たす要素だけが要素となるview
- (2):
filter_view
を生成するRangeアダプタオブジェクト
元のRangeから条件を満たす要素を探す処理は遅延評価される。
- 初めてメンバ関数
begin
が呼び出されたときに先頭の要素を決定し、残りはイテレータが進むときに求める。
begin
は償却定数時間で実行できなければならないため、begin
の値はキャッシュされる。
filter_view
の要素を書き換えてもよいが、書き換えた後の要素がPred
を満たさない場合は未定義動作を引き起こす。
borrowed |
sized |
output |
input |
forward |
bidirectional |
random_access |
contiguous |
common |
viewable |
view |
|
|
※ |
○ |
※ |
※ |
|
|
※ |
○ |
○ |
※ V
に従う
名前 |
説明 |
対応バージョン |
V base_ = V() |
元のview (説明専用) |
C++20 |
copyable-box <Pred> pred_ |
述語 (説明専用) |
C++20 C++23で削除 |
movable-box <Pred> pred_ |
述語 (説明専用) |
C++23 |
#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;
}
}
- views::filter[color ff0000]
- Clang: 13.0.0 [mark verified]
- GCC: 10.1.0 [mark verified]
- ICC: ?
- Visual C++: 2019 Update 10 [mark verified]