首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::gslice

Defined in header <valarray>

?

?

class gslice;

?

?

std::gslice的子集的选择器类。std::valarray由多个级别的跨距和大小集合定义的索引。类型对象std::gslice可用作值数组%27s的索引operator[]例如,选择多维数组的列,表示为valarray...

给定起始值s,列出了

j和尺寸清单d

J,astd::gslice由这些值构造,选择一组索引k。

J=s+Σ

J%28I

JD

J%29.

例如,具有启动索引的g片。3大踏步{19,4,1}和长度{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 + 1*19 + 3*4 + 2*1 = 36...

可以构造std::gslice对象,这些对象不止一次地选择某些索引:如果上面的示例使用{1,1,1},指数应该是{3, 4, 5, 4, 5, 6, ...}。这样的g片只能用作const版本的参数。std::valarray::operator[],否则行为就没有定义。

成员函数

(constructor)

constructs a generic slice (public member function)

startsizestride

returns the parameters of the slice (public member function)

STD::gSlice::

gslice()

?

?

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

?

?

gslice( const gslice& other );

?

?

构造一个新的泛型切片。

1%29默认构造函数。相当于gslice(0,std::valarray<std::size_t>(),std::valarray<std::size_t>())此构造函数的存在仅允许构造片数组。

2%29构造带有参数的新切片。start,,,sizes,,,strides...

3%29构造other...

参数

start

-

the position of the first element

sizes

-

an array that defines the number of elements in each dimension

strides

-

an array that defines the number of positions between successive elements in each dimension

other

-

another slice to copy

Std::片::开始,大小,步幅

std::size_t start() const;

(1)

?

std::valarray<std::size_t> size() const;

(2)

?

std::valarray<std::size_t> stride() const;

(3)

?

返回在构造开始时传递给切片的参数,分别返回大小和步长。

参数

%280%29

返回值

切片的参数--启动、大小和步幅。

复杂性

常量。

演示如何使用g片来寻址3D数组的列。

二次

代码语言:javascript
复制
#include <iostream>
#include <valarray>
void test_print(std::valarray<int>& v, int rows, int cols, int planes)
{
    for(int r=0; r<rows; ++r) {
        for(int c=0; c<cols; ++c) {
            for(int z=0; z<planes; ++z)
                std::cout << v[r*cols*planes + c*planes + z] << ' ';
            std::cout << '\n';
        }
        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 << "After column operations: \n";
    test_print(v, 2, 4, 3);
}

二次

产出:

二次

代码语言:javascript
复制
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

二次

另见

operator[]

get/set valarray element, slice, or mask (public member function)

slice

BLAS-like slice of a valarray: starting index, length, stride (class)

gslice_array

proxy to a subset of a valarray after applying a gslice (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com