| title | filter_view class (C++ Standard Library) | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| description | API reference for the Standard Template Library (STL) <ranges> filter_view class, which is a view that filters out elements of a range that don't match a predicate. | |||||||||
| ms.date | 09/27/2022 | |||||||||
| f1_keywords |
|
|||||||||
| helpviewer_keywords |
|
|||||||||
| dev_langs |
|
A view that filters out the elements of a range that don't match the predicate.
template<ranges::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>>;V
The type of the underlying range.
Pred
The type of the predicate that determines which elements to keep.
For a description of the following entries, see View class characteristics
| Characteristic | Description |
|---|---|
| Range adaptor | views::filter |
| Underlying range | Must satisfy input_range or higher |
| Element type | Same as the underlying range |
| View iterator category | input_range, forward_range, or bidirectional_range depending on the underlying range |
| Sized | No |
Is const-iterable |
No |
| Common range | Only if the underlying range satisfies common_range |
| Borrowed range | Only if the underlying range satisfies borrowed_range |
| Member functions | Description |
|---|---|
| ConstructorsC++20 | Construct the view. |
baseC++20 |
Get the underlying range. |
beginC++20 |
Get an iterator to the first element. |
endC++20 |
Get the sentinel at the end of the view. |
predC++20 |
Get a reference to the predicate that determines which elements to drop. |
| Inherited from view_interface | Description |
backC++20 |
Get the last element. |
emptyC++20 |
Test whether the view is empty. |
frontC++20 |
Get the first element. |
operator boolC++20 |
Test whether the view isn't empty. |
Header: <ranges> (since C++ 20)
Namespace: std::ranges
Compiler Option: /std:c++20 or later is required.
Construct an instance of a filter_view
1) constexpr filter_view(V base, P pred);
2) filter_view() requires default_initializable<V> && default_initializable<Pred> = default;base
The underlying view.
pred
The predicate that determines which elements to keep from the underlying view.
For information about the template parameter types, see Template parameters.
A filter_view instance.
The best way to create a filter_view is by using the views::filter range adaptor. Range adaptors are the intended way to create view classes. The view types are exposed in case you want to create your own custom view type.
1) Create a value-initialized filter_view. The predicate and the underlying view must be default-initializable.
2) Move constructs the filter_view from a base view and a pred predicate. Both base and pred are moved via std::move().
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
void print(auto v)
{
for (auto& x : v)
{
std::cout << x << ' ';
}
std::cout << '\n';
}
int main()
{
std::vector<int> v{0, 1, -2, 3, -4, -5, 6};
auto myView = std::views::filter(v, [](int i) {return i > 0; });
print(myView); // outputs 1 3 6
auto myView2 = v | std::views::filter([](int i) {return i < 3; });
print(myView2); // outputs 0 1 -2 -4 -5
}1 3 6
0 1 -2 -4 -5
Gets the underlying range.
// Uses a copy constructor to return the underlying range
constexpr V base() const& requires std::copy_constructible<V>;
// Uses std::move() to return the underlying range
constexpr V base() &&;None.
The underlying view.
Get an iterator to the first element in the view.
constexpr auto begin();An iterator pointing at the first element in the view. The behavior is undefined if the view doesn't have a predicate.
:::image type="content" source="media/begin-end-sentinel.png" alt-text="Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().":::
Get the sentinel at the end of the view.
constexpr auto end()The sentinel that follows the last element in the view:
:::image type="content" source="media/begin-end-sentinel.png" alt-text="Picture of a vector with the elements 10, 20, and 30. The first element contains 10 and is labeled begin(). The last element contains 30 and is labeled 'last element'. An imaginary box after the last element indicates the sentinel and is labeled end().":::
Get a reference to the predicate that determines which leading elements to drop.
constexpr const Pred& pred() const;A reference to the predicate.
If the class doesn't store a predicate, the behavior is undefined.
// requires /std:c++20 or later
#include <ranges>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{0, 1, 2, 3, -4, 5, 6};
auto mv = v | std::views::filter(
[](int i) {return i < 5; }); // keep the elements < 5
std::cout << std::boolalpha << mv.pred()(v[6]); // outputs "false" because v[6] = 6 and 6 is not less than 5 (the predicate)
}<ranges>
filter range adaptor
drop_while()
take_while()
view classes