C++ valarray Library - Function cshift


Description

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

Declaration

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

valarray cshift (int n) const;

C++11

valarray cshift (int n) const;

Parameters

n − It is used to find number of elements to rotate.

Return Value

It returns a copy of the valarray object with its elements rotated left n spaces.

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::cshift function.

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

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

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

   std::cout << "valarray 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 −

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