Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | valarray
(+)
 
m (fmt.)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{cpp/title|mask_array}}
 
{{cpp/title|mask_array}}
 
{{cpp/numeric/valarray/mask_array/navbar}}
 
{{cpp/numeric/valarray/mask_array/navbar}}
{{dcl begin}}
+
{{ddcl|header=valarray|
{{dcl header | valarray}}
+
{{dcl |
+
 
template< class T > class mask_array;
 
template< class T > class mask_array;
 
}}
 
}}
{{dcl end}}
 
  
{{tt|std::gslice_array}} is a helper template used by {{c|std::mask_array}} subscript operator. It has reference semantics to a subset of the array specified by an mask array ({{c|std::valarray<bool>}} object).
+
{{tt|std::mask_array}} is a helper template used by the {{rlp|operator_at|valarray subscript operator}} with {{c|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 {{c|true}} values in the {{c|std::valarray<bool>}} mask.
  
 
===Member types===
 
===Member types===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Type | Definition}}
+
{{dsc hitem|Type|Definition}}
{{dsc | {{tt|value_type}} | {{tt|T}} }}
+
{{dsc|{{tt|value_type}}|{{tt|T}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/valarray/array/dsc constructor | mask_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc constructor|mask_array}}
{{dsc inc | cpp/numeric/valarray/array/dsc destructor | mask_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc destructor|mask_array}}
{{dsc inc | cpp/numeric/valarray/array/dsc operator{{=}} | mask_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc operator{{=}}|mask_array}}
{{dsc inc | cpp/numeric/valarray/array/dsc operator_arith | mask_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc operator_arith|mask_array}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
  | output=
+
#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);
 +
}
 +
|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
 
}}
 
}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/experimental/simd/dsc simd_mask}}
 +
{{dsc end}}
 +
 +
{{langlinks|ja|zh}}

Latest revision as of 02:03, 4 October 2023

 
 
 
 
 
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.

Contents

[edit] Member types

Type Definition
value_type T

[edit] 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]

[edit] 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);
}

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

[edit] See also

(parallelism TS v2)
data-parallel type with the element type bool
(class template) [edit]