Difference between revisions of "cpp/numeric/valarray/mask array"
From cppreference.com
(+) |
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}} | ||
− | {{ | + | {{ddcl|header=valarray| |
− | + | ||
− | + | ||
template< class T > class mask_array; | template< class T > class mask_array; | ||
}} | }} | ||
− | |||
− | {{tt|std:: | + | {{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= | |
− | | 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) | |
destroys a mask_array (public member function) | |
assigns contents (public member function) | |
performs arithmetic operation on the array referred by mask. (public member function) |
[edit] Example
Run this code
#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) |