命名空间
变体
操作

std::gslice

来自 cppreference.cn
< cpp‎ | numeric‎ | valarray
 
 
 
 
定义于头文件 <valarray>
class gslice;

std::gslice 是选择器类,它通过多层次的步长和大小集来标识 std::valarray 索引的子集。std::gslice 类型的对象可以用作 valarray 的 operator[] 的索引,例如,选择表示为 valarray 的多维数组的列。

给定起始值 s,步长列表 ij 和大小列表 dj,从这些值构造的 std::gslice 选择索引集 kj=s+Σj(ijdj)

例如,一个起始索引为 3,步长为 {19,4,1},长度为 {2,4,3} 的 gslice 生成以下 24=2*4*3 个索引集

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 + 0*19 + 1*4 + 2*1 = 9,
3 + 0*19 + 2*4 + 0*1 = 11,
...
3 + 1*19 + 3*4 + 1*1 = 35,
3 + 1*19 + 3*4 + 2*1 = 36

可以构造 std::gslice 对象,使其多次选择某些索引:如果上面的例子使用步长 {1,1,1},则索引将是 {3, 4, 5, 4, 5, 6, ...}。这样的 gslice 只能用作 std::valarray::operator[] 的 const 版本的参数,否则行为是未定义的。

目录

[编辑] 成员函数

(构造函数)
构造一个通用切片
(公开成员函数)
startsizestride
返回切片的参数
(公开成员函数)

std::gslice::gslice

gslice()
(1)
gslice( std::size_t start, const std::valarray<std::size_t>& sizes,
                           const std::valarray<std::size_t>& strides );
(2)
gslice( const gslice& other );
(3)

构造一个新的通用切片。

1) 默认构造函数。等同于 gslice(0, std::valarray<std::size_t>(), std::valarray<std::size_t>())。此构造函数仅用于允许构造切片数组。
2) 用参数 startsizesstrides 构造一个新的切片。
3) 构造 other 的副本。

参数

start - 第一个元素的位置
sizes - 定义每个维度中元素数量的数组
strides - 定义每个维度中连续元素之间位置数量的数组
其他 - 要复制的另一个切片


std::slice::start, size, stride

std::size_t start() const;
(1)
std::valarray<std::size_t> size() const;
(2)
std::valarray<std::size_t> stride() const;
(3)

返回在构造时传递给切片的参数——分别是起始、大小和步长。

参数

(无)

返回值

切片的参数——分别是起始、大小和步长。

复杂度

常数时间。

[编辑] 示例

演示使用 gslice 寻址三维数组的列

#include <iostream>
#include <valarray>
 
void test_print(std::valarray<int>& v, int planes, int rows, int cols)
{
    for (int r = 0; r < rows; ++r)
    {
        for (int z = 0; z < planes; ++z)
        {
            for (int c = 0; c < cols; ++c)
                std::cout << v[z * rows * cols + r * cols + c] << ' ';
            std::cout << "  ";
        }
        std::cout << '\n';
    }
}
 
int main()
{
    std::valarray<int> v = // 3d array: 2 x 4 x 3 elements
        {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
    v[std::gslice(0, {2, 4}, {4 * 3, 3})] = 1; // two level one strides of 12 elements
                                               // then four level two strides of 3 elements
 
    // 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 << "\n" "After column operations:\n";
    test_print(v, 2, 4, 3);
}

输出

Initial 2x4x3 array:
111 112 113   211 212 213
121 122 123   221 222 223
131 132 133   231 232 233
141 142 143   241 242 243
 
After column operations:
1 -1 113   1 212 213
1 -1 123   1 222 223
1 -1 133   1 232 233
1 -1 143   1 242 243

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 543 C++98 不清楚默认构造的通用切片是否可用 它可用(作为空子集)

[编辑] 另请参阅

获取/设置 valarray 元素、切片或掩码
(public member function) [编辑]
valarray 的类 BLAS 切片:起始索引、长度、步长
(class) [编辑]
应用 gslice 后 valarray 子集的代理
(class template) [编辑]