public member function

std::valarray::shift

<valarray>
valarray<T> shift (int n) const;
Shift elements
Returns a valarray with its elements shifted left n spaces (or right if n is negative).

The valarray returned has the same length as *this, with the new elements intialized with their default constructors.

By shifting an array, 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 length) or a newly constructed element (if I+n is greater than the length).

Unlike with 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 valarray object with the elements of *this shifted n spaces.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// valarray::shift example
#include <iostream>
#include <valarray>
using namespace std;

int main ()
{
  int init[]={10,20,30,40,50};

  valarray<int> myvalarray (init,5);   // 10 20 30 40 50
  myvalarray = myvalarray.shift(2);    // 30 40 50  0  0
  myvalarray = myvalarray.shift(-1);   // 0  30 40 50  0

  for (size_t n=0; n<myvalarray.size(); n++)
	  cout << myvalarray[n] << ' ';
  cout << endl;

  return 0;
}


Output:

0 30 40 50 0

See also