📜  std :: gslice | Valarray广义切片选择器

📅  最后修改于: 2021-05-30 02:51:13             🧑  作者: Mango

Valarray通用切片选择器:此类表示valarray通用切片选择器(多维切片)。它不包含也没有引用任何元素–它仅描述要用作valarray :: 运算符[]中的索引的元素的选择。

换句话说, std :: gslice是选择器类,用于标识由多级步幅和大小集定义的std :: valarray索引的子集。 std :: gslice类型的对象可用作valarray的运算符[]的索引,以选择例如表示为valarray的多维数组的列。

给定起始值s,一个步幅i j列表和大小d j列表,从这些值构造的std :: gslice选择索引集:

kj = s + Σj(ijdj)

valarray广义切片由起始索引,一组大小和一组跨度指定。它产生切片选择的多维组合。

句法 :

gslice( std::size_t start, const std::valarray& sizes,
                           const std::valarray& strides );
size_t start : index of the first element in the selection.
size_t (size) : number of elements in the selection.
size_t (stride) : span that separates the elements selected
  • 在给定的语法中,默认构造函数等于gslice(0,std :: valarray(),std :: valarray())。存在此构造方法仅是为了构造切片数组。
  • 构造一个新的切片,其参数为start,size,stride。

范例1:

start = 1 , size = {2, 3} , stride = {7, 2}
Input :  0 1 2 3 4 5 6 7 8 9 10 11 12 13
Output : 1 3 5 8 10 12
Explanation: 1 + 0*7 + 0*2 = 1,
             1 + 0*7 + 1*2 = 3,
             1 + 0*7 + 2*2 = 5,
             1 + 1*7 + 0*2 = 8,
             1 + 1*7 + 1*2 = 10,
             1 + 1*7 + 2*2 = 12

范例2:

start = 3 , size = {2,4,3} , strides = {19,4,1}
Input : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Output: 3 4 5 7 8 9 11 12 13 15 16 17 19 20 21 22
Explanation : 3 + 0*19 + 0*4 + 0*1 = 3,
              3 + 0*19 + 0*4 + 1*1 = 4,
              3 + 0*19 + 0*4 + 2*1 = 5,
              3 + 0*19 + 1*4 + 0*1 = 7,
              3 + 0*19 + 1*4 + 1*1 = 8,
              ...
              ...
              3 + 1*19 + 0*4 + 0*1 = 22 

示例1:C++程序中的gslice示例:

// C++ Program to test the 
// functioning of std::gslice
#include      // std::cout
#include       // std::size_t
#include      // std::valarray, std::gslice
  
int main ()
{
  //valarray sample of size 14
  std::valarray sample (14);
  for (int i=0; i<14; ++i) sample[i]=i;
  
  std::size_t start=1;
  std::size_t lengths[]= {2,3};
  std::size_t strides[]= {7,2};
    
  //gslice object which can be used directly
  std::gslice mygslice (start,
                        std::valarray(lengths,2),
                        std::valarray(strides,2));
  //creating data using gslice
  std::valarray data = sample[mygslice];
  
 /* Can also be done in the following way 
 (without creating gslice object): 
 std::valarray data=sample[std::gslice(start, 
                       std::valarray(lengths,2),
                       std::valarray(strides,2))];
  */  
  
  std::cout << "gslice:";
    
//displaying content of data 
  for (int i=0; i

输出:

gslice: 1 3 5 8 10 12

示例2:演示使用gslice寻址3D数组的列并执行一些操作

//C++ Program to demonstrate use of 
// gslice to address columns of 3D array
#include  // std::cout
#include  // std::gslice
void test_print(std::valarray& v, int rows, int cols, int planes)
{
    for(int r=0; r v = 
    { 111,112,113 , 121,122,123 , 131,132,133 , 141,142,143,
     211,212,213 , 221,222,223 , 231,232,233 , 241,242,243};
     
 // int ar3D [2][4][3]
    std::cout << "Initial 2x4x3 array:\n";
    test_print(v, 2, 4, 3);
   
    // update every value in the first columns of both planes
    // two level one strides of 12 elements
    // then four level two strides of 3 elements
    v[std::gslice(0, {2, 4}, {4*3, 3})] = 1; 
     
   
    // subtract the third column from the 
    // second column in the 1st plane
    v[std::gslice(1, {1, 4}, {4*3, 3})] 
        -= v[std::gslice(2, {1, 4}, {4*3, 3})];
   
    std::cout << "After column operations: \n";
    test_print(v, 2, 4, 3);
}

输出:

Initial 2x4x3 array:
111 112 113 
121 122 123 
131 132 133 
141 142 143 

211 212 213 
221 222 223 
231 232 233 
241 242 243 

After column operations: 
1 -1 113 
1 -1 123 
1 -1 133 
1 -1 143 

1 212 213 
1 222 223 
1 232 233 
1 242 243 

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”