public member function
<valarray>
std::valarray::cshiftvalarray cshift (int n) const;
Circularly shift elements
Returns a copy of the valarray object 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 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
.
*this
rotated n spaces left.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// valarray::cshift example
#include <iostream> // std::cout
#include <cstddef> // std::size_t
#include <valarray> // std::valarray
int main ()
{
int init[]={10,20,30,40,50};
std::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
std::cout << "myvalarray contains:";
for (std::size_t n=0; n<myvalarray.size(); n++)
std::cout << ' ' << myvalarray[n];
std::cout << '\n';
return 0;
}
myvalarray contains: 20 30 40 50 10
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4