indirect_array
class template
<valarray>
template <class T> indirect_array;
Valarray indirect 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 a valarray of indexes.
It contains references to the elements in the valarray object whose indexes are part of the valarray, 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 indirect_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 indirect_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&);
~indirect_array();
private:
indirect_array();
indirect_array(const indirect_array&);
indirect_array& operator= (const indirect_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
|
// indirect_array example
#include <iostream>
#include <valarray>
using namespace std;
int main ()
{
valarray<int> myarray (8);
for (int i=0; i<8; ++i) myarray[i]=i; // 0 1 2 3 4 5 6 7
size_t sel[] = {3,5,6};
valarray<size_t> selection (sel,3); // * * *
myarray[selection] *= valarray<int>(10,3); // 0 1 2 30 4 50 60 7
myarray[selection] = 0; // 0 1 2 0 4 0 0 7
cout << "myarray:\n";
for (size_t i=0; i<myarray.size(); ++i)
cout << myarray[i] << ' ';
cout << endl;
return 0;
}
|
Output:
See also
mask_array | Valarray mask selection (class template) |
|