std::slice_array
提供: cppreference.com
ヘッダ <valarray> で定義
|
||
template< class T > class slice_array; |
||
std::slice_array
は std::slice の添字演算子によって使用されるヘルパーテンプレートです。 std::slice
オブジェクトによって指定される配列の部分集合への参照の意味論を持ちます。
目次 |
メンバ型
型 | 定義 |
value_type
|
T
|
メンバ関数
slice_array を構築します (パブリックメンバ関数) | |
slice_array を破棄します (パブリックメンバ関数) | |
内容を代入します (パブリックメンバ関数) | |
スライスによって参照される配列に対して算術演算を行います (パブリックメンバ関数) |
例
Run this code
#include <iostream> #include <valarray> class Matrix { int dim; std::valarray<int> data; public: explicit Matrix(int dim, int init = 0) : dim{dim}, data(init, dim*dim) {} void clear(int value = 0) { data = value; } void identity() { clear(); diagonal() = 1; } int& operator()(int x, int y) { return data[dim * y + x]; } std::slice_array<int> diagonal() { return data[std::slice(0, dim, dim+1)]; } std::slice_array<int> secondary_diagonal() { return data[std::slice(dim-1, dim, dim-1)]; } std::slice_array<int> row(std::size_t row) { return data[std::slice(dim*row, dim, 1)]; } std::slice_array<int> column(std::size_t col) { return data[std::slice(col, dim, dim)]; } void print(char term = '\n') const { for (unsigned i = 0; i != data.size(); ++i) std::cout << data[i] << (((i+1) % dim) ? ' ' : '\n'); std::cout << term; } }; int main() { Matrix m{3}; m.identity(); m.print(); m.clear(1); m.secondary_diagonal() = 3; m.print(); m.clear(); m.row(1) = std::valarray<int>{4, 5, 6}; m.print(); m.clear(); m.column(1) = std::valarray<int>{7, 8, 9}; m.print(); }
出力:
1 0 0 0 1 0 0 0 1 1 1 3 1 3 1 3 1 1 0 0 0 4 5 6 0 0 0 0 7 0 0 8 0 0 9 0
関連項目
gslice 適用後の valarray のサブセットへのプロキシ (クラステンプレート) |