std::mask_array

Header: <valarray>

std::mask_array is a helper template used by the valarray subscript operator with std::valarray argument. It has reference semantics and provides access to the subset of the valarray consisting of the elements whose indices correspond to true values in the std::valarray mask.

# Declarations

template< class T > class mask_array;

# Example

#include <iostream>
#include <valarray>
 
void println(auto rem, const auto& data)
{
    for (std::cout << rem; int n : data)
        std::cout << n << ' ';
    std::cout << '\n';
}
 
int main()
{
    std::valarray<int> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 
    println("Initial valarray: ", data);
 
    data[data > 5] = -1;
    // the type of data>5 is std::valarray<bool>
    // the type of data[data>5] is std::mask_array<int>
 
    println("After v[v>5]=-1:  ", data);
}

# See also