|
mask_array
class template
<valarray>
template <class T> mask_array;
Valarray mask selection
This class is used as an intermediate type returned by the non-constant version of valarray's subscript operator (valarray operator[] ) when used with masks.
It contains references to the elements in the valarray object that are selected by a mask, and overloads the assignment and compound assignment operators, allowing direct access to the elements in the selection when used as l-value (left-value of an assignment operation).
When used as r-value (right-value of an assignment operation), it can initialize a valarray object, since valarray has a constructor taking a mask_array object as argument.
All the constructors of this class are private, preventing direct instantiations by a program - its only purpose is to be an efficient way to access the elements selected by a mask with valarray's operator[] .
It is declared as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
template <class T> class mask_array {
public:
typedef T value_type;
void operator= (const valarray<T>&) const;
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<= (const valarray<T>&) const;
void operator>>= (const valarray<T>&) const;
void operator=(const T&);
~mask_array();
private:
mask_array();
mask_array(const mask_array&);
mask_array& operator= (const mask_array&);
};
|
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
// mask_array example
#include <iostream>
#include <valarray>
using namespace std;
int main ()
{
valarray<int> myarray (10);
for (int i=0; i<10; ++i) myarray[i]=i; // 0 1 2 3 4 5 6 7 8 9
valarray<bool> mymask (10);
for (int i=0; i<10; ++i)
mymask[i]= ((i%2)==1); // f t f t f t f t f t
myarray[mymask] *= valarray<int>(10,5); // 0 10 2 30 4 50 6 70 8 90
myarray[!mymask] = 0; // 0 10 0 30 0 50 0 70 0 90
cout << "myarray:\n";
for (size_t i=0; i<myarray.size(); ++i)
cout << myarray[i] << ' ';
cout << endl;
return 0;
}
|
Output:
See also
|