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

std::slice

Defined in header <valarray>

?

?

class slice;

?

?

std::slice的子集的选择器类。std::valarray类似于布拉斯切片。类型对象std::slice保存三个值:起始索引、步幅和子集中的值总数。类型对象std::slice可用作值数组%27s的索引operator[]...

成员函数

(constructor)

constructs a slice (public member function)

startsizestride

returns the parameters of the slice (public member function)

STD::片::片

slice()

?

?

slice( std::size_t start, std::size_t size, std::size_t stride );

?

?

slice( const slice& other );

?

?

构造一个新的切片。

1%29默认构造函数。相当于slice(0, 0, 0)此构造函数的存在仅允许构造片数组。

2%29构造带有参数的新切片。start,,,size,,,stride.此切片将指size每个有职位的元素数:

开始+0%2A步幅

Start+1%2A步幅

...

+%28大小-1%29%2A步幅

3%29构造other...

参数

start

-

the position of the first element

size

-

the number of elements in the slice

stride

-

the number of positions between successive elements in the slice

other

-

another slice to copy

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

std::size_t start() const;

(1)

?

std::size_t size() const;

(2)

?

std::size_t stride() const;

(3)

?

返回分别传递给构造开始、大小和步长的参数。

参数

%280%29

返回值

切片的参数分别为起始、尺寸和步长。

复杂性

常量。

类的基本值数组支持矩阵类。痕迹%28http://en.wikipara.com/wiki/Trace_%28线性代数%29%29计算函数

二次

代码语言:javascript
复制
#include <iostream>
#include <valarray>
class Matrix {
    std::valarray<int> data;
    int dim;
 public:
    Matrix(int r, int c) : data(r*c), dim(c) {}
    int& operator()(int r, int c) {return data[r*dim + c];}
    int trace() const {
        return data[std::slice(0, dim, dim+1)].sum();
    }
};
int main()
{
    Matrix m(3,3);
    int n = 0;
    for(int r=0; r<3; ++r)
       for(int c=0; c<3; ++c)
           m(r, c) = ++n;
    std::cout << "Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is " << m.trace() << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is 15

二次

另见

operator[]

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

gslice

generalized slice of a valarray: starting index, set of lengths, set of strides (class)

slice_array

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

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com