名前空間
変種
操作

std::slice

From cppreference.com
< cpp‎ | numeric‎ | valarray
 
 
 
 
ヘッダ <valarray> で定義
class slice;

std::slice は、std::valarray のサブセットを識別するためのセレクタクラスです。これは BLAS のスライスに似ています。std::slice 型のオブジェクトは 3 つの値、つまり開始インデックス、ストライド、およびサブセット内の値の総数を保持します。std::slice 型のオブジェクトは、valarray の operator[] でインデックスとして使用できます。

目次

[編集] メンバ関数

(コンストラクタ)
スライスを構築します
(public member function)
startsizestride
スライスのパラメータを返します
(public member function)

std::slice::slice

slice()
(1)
slice( std::size_t start, std::size_t size, std::size_t stride );
(2)
slice( const slice& other );
(3)

新しいスライスを構築します。

1) デフォルトコンストラクタ。slice(0, 0, 0) と同等です。このコンストラクタは、スライスの配列を構築できるようにするためにのみ存在します。
2) パラメータ startsizestride を持つ新しいスライスを構築します。このスライスは、size 個の要素を参照し、各要素の位置は次のようになります。
start + 0 * stride
start + 1 * stride
...
start + (size - 1) * stride
3) other のコピーを構築します。

パラメータ

start - 最初の要素の位置
size - スライス内の要素数
stride - スライス内の連続する要素間の位置数
その他 - コピーする別のスライス

std::slice::start, size, stride

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

スライス構築時に渡されたパラメータ(それぞれ start、size、stride)を返します。

パラメータ

(なし)

戻り値

スライスのパラメータ — それぞれ start、size、stride。

計算量

定数。

[編集] 非メンバ関数

operator==(std::slice)
(C++20)
2 つのスライスが等しいかどうかをチェックします
(関数)

operator==(std::slice)

friend bool operator==( const slice& lhs, const slice& rhs );
(C++20以降)

lhsrhs のパラメータ(start、size、stride)がそれぞれ等しいかどうかをチェックします。

この関数は、通常の 修飾されていない または 修飾された 検索からは見えず、std::slice が引数の関連クラスである場合にのみ 引数依存の名前探索 によって見つけることができます。

!= 演算子は operator== から合成される。

パラメータ

lhs, rhs - 比較するスライス

戻り値

lhs.start() == rhs.start() && lhs.size() == rhs.size() && lhs.stride() == rhs.stride()

[編集]

トレース計算関数を備えた、ベアボーンの valarray ベースの Matrix クラス。

#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';
}

出力

Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is 15

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 543 C++98 デフォルト構築されたスライスが使用可能かどうか不明でした。 使用可能です(空のサブセットとして)。

[編集] 関連項目

valarrayの要素、スライス、またはマスクを取得/設定する
(public member function) [編集]
valarrayの汎用スライス: 開始インデックス、長さの集合、ストライドの集合
(class) [編集]
スライスを適用した後のvalarrayのサブセットへのプロキシ
(class template) [編集]
(C++23)
多次元の所有権を持たない配列ビュー
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)