gslice definition in C++
I have some misunderstanding about the gslice
function. Definition from MSDN states:
gslice defines a subset of a valarray that consists of multiple slices of the valarray that each start at the same specified element. The ability to use arrays to define multiple slices is the only difference between gslice and slice::slice. The first slice has a first element with an index of开发者_运维问答 _StartIndex, a number of elements specified by the first element of _LenArray, and a stride given by the first element of _IncArray. The next set of orthogonal slices has first elements given by the first slice. The second element of _LenArray specifies the number of elements. The stride is given by the second element of _IncArray. A third dimension of slices would take the elements of the two-dimensional array as the starting elements and proceed analogously.
So suppose I have the following code.
#include <iostream>
#include <valarray>
using namespace std;
int main(){
valarray<int>v(12),result;
valarray<size_t>len(2),interval(2);
for (int i=0;i<12;i++)
v[i]=i;
len[0]=3;
len[1]=3;
interval[0]=2;
interval[1]=3;
result=v[gslice(0,len,interval)];
for (int i=0;i< result.size();i++)
cout<<result[i]<< " ";
return 0;
}
As I understand 0 indicate first element of valarray. Then len
indicates that len[0] or 3 is the distance from before element and len[1] or 3 is number of elements. Then interval[0]
or 2 indicates first element of next set and interval[1]. Or 3 distance from before element so result should be like this
0 3 6 2 5 8
It is so but here are additional elements:
4 7 10
And why?
See this description from cplusplus.com. It includes a diagram as to what the function is actually doing.
精彩评论