Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/valarray/mask array"

From cppreference.com
< cpp‎ | numeric‎ | valarray
m (link to zh)
m (link to ja)
Line 51: Line 51:
 
}}
 
}}
  
{{langlinks|zh}}
+
{{langlinks|ja|zh}}

Revision as of 06:56, 6 July 2018

 
 
 
 
 
Defined in header <valarray>
template< class T > class mask_array;

std::mask_array is a helper template used by the valarray subscript operator with std::valarray<bool> 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<bool> mask.

Member types

Type Definition
value_type T

Member functions

constructs a mask_array
(public member function) [edit]
destroys a mask_array
(public member function) [edit]
assigns contents
(public member function) [edit]
performs arithmetic operation on the array referred by mask.
(public member function) [edit]

Example

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

Output:

Initial valarray: 0 1 2 3 4 5 6 7 8 9 
After v[v>5]=-1:  0 1 2 3 4 5 -1 -1 -1 -1