- 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 |
| 名前 |
説明 |
対応バージョン |
operator bool |
Rangeが空でないかどうかを判定する |
C++20 |
front |
先頭要素への参照を取得する |
C++20 |
back |
末尾要素への参照を取得する |
C++20 |
empty |
Rangeが空かどうかを判定する |
C++20 |
cbegin |
定数イテレータを取得する |
C++23 |
cend |
定数イテレータ(番兵)を取得する |
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]