class template
std::slice_array
<valarray>
template <class T> slice_array;
Valarray slice 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
slices.
It contains references to the elements in the
valarray object that are selected in a slice, 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
slice_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 slice 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 slice_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&);
~slice_array();
private:
slice_array();
slice_array(const slice_array&);
slice_array& operator= (const slice_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 25
|
// slice_array example
#include <iostream>
#include <valarray>
using namespace std;
int main ()
{
valarray<int> foo (9);
slice myslice;
for (int i=0; i<9; ++i) foo[i]=i; // 0 1 2 3 4 5 6 7 8
// | | |
myslice=slice(1,3,2); // v v v
foo[myslice] *= valarray<int>(10,3); // 0 10 2 30 4 50 6 7 8
// | | |
myslice = slice (0,3,3); // v v v
foo[myslice] = 99; // 99 10 2 99 4 50 99 7 8
cout << "foo:\n";
for (size_t n=0; n<foo.size(); n++)
cout << foo[n] << ' ';
cout << endl;
return 0;
}
|
Output:
See also
- gslice_array
- Valarray gslice selection (class template)
- mask_array
- Valarray mask selection (class template
)
- indirect_array
- Valarray indirect selection (class template)