C++ valarray Library - Function shift



Description

It returns a copy of the valarray object with its elements shifted left n spaces (or right if n is negative).

Declaration

Following is the declaration for std::valarray::shift function.

valarray shift (int n) const;

C++11

valarray shift (int n) const;

Parameters

n − It is contains the information about number of elements to shift.

Return Value

none

Exceptions

Basic guarantee − if any operation performed on the elements throws an exception.

Data races

All elements effectively copied are accessed.

Example

In below example explains about std::valarray::shift function.

#include <iostream>
#include <cstddef>
#include <valarray>

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

   std::valarray<int> myvalarray (init,5);
   myvalarray = myvalarray.shift(2);
   myvalarray = myvalarray.shift(-1);

   std::cout << "myvalarray contains:";
   for (std::size_t n=0; n<myvalarray.size(); n++)
      std::cout << ' ' << myvalarray[n];
   std::cout << '\n';

   return 0;
}

Let us compile and run the above program, this will produce the following result −

myvalarray contains: 0 20 30 40 0
valarray.htm
Advertisements