public member function
<valarray>
std::valarray::shift
valarray shift (int n) const;
Shift elements
The valarray returned has the same length as *this, with the new elements value-initialized.
By shifting a valarray, the I-th element in the resulting valarray corresponds to the I+n-th element in the original valarray (whenever I+n is less than its size) or a default-constructed element (if I+n is greater than the size).
Unlike valarray::cshift (circular shift), the valarray returned by shift does not include the first n elements in *this (or the last -n elements if n is negative) in any position.
Parameters
- n
- Number of elements to shift. If positive, it is shifted left. If negative, it is shifted right.
Return value
A new valarray object with the elements of*this shifted n spaces left.Example
|
|
Output:
myvalarray contains: 0 30 40 50 0
Complexity
Depends on library implementation (operations may be parallelized).Iterator validity
No changes: Valid iterators, references and sub-arrays of the source valarray keep their validity.Data races
Both the valarray and some of its elements are accessed.Exception safety
If the function implementation assigns to elements, and any such operation throws an exception, it causes undefined behavior.If the function needs to allocate internal storage and fails, it may throw an exception (such as bad_alloc), although this is not mandated.
See also
- valarray::cshift
- Circularly shift elements (public member function)
- valarray::resize
- Resize valarray (public member function)
- valarray operators
- Valarray operators (function)