std::gslice_array
来自cppreference.com
在标头 <valarray> 定义 | ||
template<class T > class gslice_array; | ||
std::gslice_array
是接受 std::gslice 实参的 valarray
下标运算符所用的辅助模板。它拥有到 std::gslice 对象所指定的数组子集的引用语义。
目录 |
[编辑]成员类型
类型 | 定义 |
value_type | T |
[编辑]成员函数
构造一个 gslice_array (公开成员函数) | |
销毁一个 gslice_array (公开成员函数) | |
赋值内容 (公开成员函数) | |
在通用切片所指代的数组上进行算术运算。 (公开成员函数) |
[编辑]示例
运行此代码
#include <cassert>#include <cstddef>#include <iomanip>#include <iostream>#include <numeric>#include <valarray> int main(){std::valarray<int> data(32);std::iota(std::begin(data), std::end(data), 0); conststd::size_t offset =1, z =2, y =3, x =4;conststd::valarray<std::size_t> sizes{z, y, x};conststd::valarray<std::size_t> strides{15, 5, 1};conststd::gslice gslice =std::gslice(offset, sizes, strides);// 根据公式生成索引:// index[k] = offset + [0,1,2)*15 + [0,1,2,3)*5 + [0,1,2,3,4)*1// = offset + inner_product(sizes[k], strides);// 其中 sizes[k] = {[0,z), [0,y), [0,x)},最右侧索引 (x) 变化最快。// 结果是一下索引集合:// index[0] = 1 + 0*15 + 0*5 + 0*1 = 1// index[1] = 1 + 0*15 + 0*5 + 1*1 = 2// index[2] = 1 + 0*15 + 0*5 + 2*1 = 3// index[3] = 1 + 0*15 + 0*5 + 3*1 = 4// index[4] = 1 + 0*15 + 1*5 + 0*1 = 6// index[5] = 1 + 0*15 + 1*5 + 1*1 = 7// index[6] = 1 + 0*15 + 1*5 + 2*1 = 8// index[7] = 1 + 0*15 + 1*5 + 3*1 = 9// ...// index[22] = 1 + 1*15 + 2*5 + 2*1 = 28// index[23] = 1 + 1*15 + 2*5 + 3*1 = 29 conststd::valarray<int> indices = data[gslice];for(unsigned i =0; i != indices.size();++i)std::cout<<std::setfill('0')<<std::setw(2)<< indices[i]<<' ';std::cout<<"\n索引总数: "<< indices.size()<<'\n';assert(indices.size()== x * y * z); data =0; std::gslice_array<int> gslice_array = data[gslice]; gslice_array =1;// 对应于生成的索引的单元格 = '1', 跳过的单元格 = '0'.for(auto i : data)std::cout<< i <<' ';std::cout<<"\n1 的求和 = "<< data.sum()<<'\n';}
输出:
01 02 03 04 06 07 08 09 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29 索引总数: 24 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 1 的求和 = 24
[编辑]参阅
到 valarray 应用切片后的子集的代理 (类模板) | |
(C++23) | 多维非拥有数组视图 (类模板) |