Apache C++ Standard Library Reference Guide

## slice

Library: Numerics

    Does not inherit

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Constructors
- Accessors
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

size() | slice() | start() | stride()

### Summary

A numeric array class for representing a BLAS-like slice from an array

### Synopsis

    #include <valarray>

    namespace std {
      class slice ;
    }

### Description

The valarray helper class slice allows you to represent a BLAS-like slice from an array. A BLAS slice contains a starting index, a length, and a stride. The index indicates the first element in the slice, the length determines the number of elements, and the stride indicates the interval between elements in the original array. For instance, the slice (1,3,2) applied to the array (1,2,3,4,5,6,7) produces the array (2,4,6).

When applied to a valarray using the slice subscript operator (see valarray) a slice produces a slice_array. The slice_array gives a view into the original valarray that is tailored to match parameters of the slice. The elements in a slice_array are references to the elements in the original array. This means you need to explicitly copy the slice_array into another valarray in order to have a distinct array.

### Interface

    namespace std {

      class slice {
      public:
        // Constructors
        slice();
        slice(size_t, size_t, size_t);
        slice(const slice&)

        // Accessors
        size_t start() const;
        size_t size() const;
        size_t stride() const;
      };
    }

### Constructors

    slice();

Creates a slice specifying no elements. This constructor is only intended to allow the creation of arrays of slices.

    slice(size_t start, size_t length, size_t stride);

Creates a slice with starting index, length, and stride as indicated by the arguments.

    slice(const slice&)

Creates a slice with starting index, length, and stride as indicated by the slice argument.

### Accessors

    size_t start() const;

Returns the starting index of the slice.

    size_t size() const;

Returns the length of the slice.

    size_t stride() const;

Returns the stride of the slice.

### Example

    //
    //  slice.cpp
    //

    #include <valarray.h>  // Includes valarray and provides stream inserter.

    typedef std::valarray<int> valarray_t;

    int main(void) {

      // Create a valarray of ints.
      valarray_t::value_type ibuf[10] = {0,1,2,3,4,5,6,7,8,9};
      valarray_t vi(ibuf,10);

      // Print it out.
      std::cout << "original valarray vi:\n\n" << vi << "\n\n";

      // Print out a slice.
      std::cout << "vi[slice(1,3,2)]\n\n"
                << vi[std::slice(1,3,2)] << std::endl;

      return 0;
    }

    Program Output:

    original valarray vi:

    [0,1,2,3,4,5,6,7,8,9]

    vi[slice(1,3,2)]

    [1,3,5]

### See Also

valarray, slice_array, gslice, gslice_array, mask_array, indirect_array

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 26.3.4