cplusplus.com
C++ : Reference : Miscellaneous : valarray : valarray : cshift
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
valarray
classes:
gslice
gslice_array
indirect_array
mask_array
slice
slice_array
valarray
global functions:
abs
acos
asin
atan
atan2
cos
cosh
exp
log
log10
pow
sin
sinh
sqrt
tan
tanh
valarray
valarray operators
valarray::valarray
valarray::~valarray
member functions:
valarray::apply
valarray::cshift
valarray::max
valarray::min
valarray::operator=
valarray::operator[]
valarray::resize
valarray::shift
valarray::size
valarray::sum


valarray::cshift

public member function
valarray<T> cshift (int n) const;

Cyclically shift elements

Returns a valarray with its elements rotated left n spaces (or right if n is negative).

The valarray returned has the same length as *this.

By cyclically shifting (rotating) an array, the I-th element in the resulting valarray corresponds to the element with position (I+n)%size() in the original valarray.

Unlike with valarray::shift, the valarray returned by cshift does not initialize new elements with their default constructor to fill the
last n elements in *this (or the first -n elements if n is negative). Instead, it uses the first (or last) elements of *this.

Parameters

n
Number of elements to rotate. If positive, it is rotated left. If negative, it is rotated right.

Return value

A valarray object with the elements of *this rotated n spaces.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// valarray::cshift 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.cshift(2);   // 30 40 50 10 20
  myvalarray = myvalarray.cshift(-1);  // 20 30 40 50 10

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

  return 0;
}


Output:

20 30 40 50 10

See also