Apache C++ Standard Library User's Guide

## 22.6 Member Functions

The member functions of class valarray include the `size()` function, which returns the size of a valarray, and the `sum()` function, which returns the sum of all elements in a valarray. The functions `min()` and `max()` return, respectively, the minimum and maximum values in a valarray.

Class valarray also contains two shift functions. Both functions shift elements a specified number of steps to the left, where the `0`th element is defined as the leftmost. The `shift()` function fills in at the right with `0`s or `T()`s. The `cshift()` function performs a circular shift or rotation so that the `i`th element becomes the element at location `self.length() - n - i`, where `i` runs from `0` to `n`, and `n` is the number of steps to shift. Both functions return a new valarray, but the original is not changed. For example:

    int a[5] = {1,2,3,4,5};
    std::valarray<int> v(a,5);
    std::valarray<int> v2 = v.shift(2);           // v2 = {3,4,5,0,0}
    v2 = v.cshift(2);                            // v2 = {3,4,5,1,2}

Class valarray provides two versions of the `apply()` function. These functions apply a user-specified function to each element in the array. Again, these return a new array with the result leaving the original array unaltered.

Finally, as we noted in the discussion on constructors, valarray has a resize function that allows a program to change the number of elements contained by that array. The original elements, if any, are lost in the process since the function re-initializes all elements in the newly-sized array.