Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/valarray/slice"

From cppreference.com
< cpp‎ | numeric‎ | valarray
m (Text replace - "{{cpp|" to "{{c|")
(changed order of parameter for ctr)
Line 11: Line 11:
 
{{dcl list mem ctor | cpp/numeric/valarray/slice/slice | constructs a slice}}
 
{{dcl list mem ctor | cpp/numeric/valarray/slice/slice | constructs a slice}}
 
{{dcl list mem fun | cpp/numeric/valarray/slice/start | accesses the start of the slice}}
 
{{dcl list mem fun | cpp/numeric/valarray/slice/start | accesses the start of the slice}}
{{dcl list mem fun | cpp/numeric/valarray/slice/stride | accesses the stride of the slice}}
 
 
{{dcl list mem fun | cpp/numeric/valarray/slice/size | accesses the size of the slice}}
 
{{dcl list mem fun | cpp/numeric/valarray/slice/size | accesses the size of the slice}}
 +
{{dcl list mem fun | cpp/numeric/valarray/slice/stride | accesses the stride of the slice}}
 
{{dcl list end}}
 
{{dcl list end}}
  

Revision as of 01:05, 25 April 2012

Template:cpp/numeric/valarray/sidebar

Defined in header <valarray>
class slice;

std::slice is the selector class that identifies a subset of std::valarray similar to BLAS slice. An object of type std::slice holds three values: the starting index, the stride, and the total number of values in the subset. Objects of type std::slice can be used as indexes with valarray's operator[].

Member functions

constructs a slice
(public member function)
accesses the start of the slice
(public member function)
accesses the size of the slice
(public member function)
accesses the stride of the slice
(public member function)

Example

Barebones valarray-backed Matrix class with a trace calculating function.

#include <iostream>
#include <valarray>
class Matrix {
    std::valarray<int> data;
    int dim;
 public:
    Matrix(int r, int c) : data(r*c), dim(c) {}
    int& operator()(int r, int c) {return data[r*dim + c];}
    int trace() const {
        return data[std::slice(0, dim, dim+1)].sum();
    }
};
int main()
{
    Matrix m(3,3);
    int n = 0;
    for(int r=0; r<3; ++r)
       for(int c=0; c<3; ++c)
           m(r, c) = ++n;
    std::cout << "Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is " << m.trace() << '\n';
}

Output:

Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is 15

See also

Template:cpp/numeric/valarray/dcl list operator atTemplate:cpp/numeric/valarray/dcl list gsliceTemplate:cpp/numeric/valarray/dcl list slice array