std:: slice_array
From cppreference.net
|
定义于头文件
<valarray>
|
||
|
template
<
class
T
>
class
slice_array
;
|
||
std::slice_array
是由
valarray下标运算符
与
std::
slice
参数配合使用的辅助模板。它具有对由
std::
slice
对象指定的数组子集的引用语义。
目录 |
成员类型
| 类型 | 定义 |
value_type
|
T
|
成员函数
构造
slice_array
对象
(公开成员函数) |
|
销毁
slice_array
对象
(公开成员函数) |
|
|
赋值内容
(公开成员函数) |
|
|
对切片引用的数组执行算术运算
(公开成员函数) |
示例
运行此代码
#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)]; } template<unsigned, unsigned> friend class MatrixStack; }; template<unsigned dim = 3, unsigned max = 8> class MatrixStack { std::valarray<int> stack; unsigned count = 0; public: MatrixStack() : stack(dim * dim * max) {} void print_all() const { std::valarray<int> row(dim*count); for (unsigned r = 0; r != dim; ++r) // 屏幕行 { row = stack[std::gslice(r * dim, {count, dim}, {dim * dim, 1})]; for (unsigned i = 0; i != row.size(); ++i) std::cout << row[i] << ((i + 1) % dim ? " " : " │ "); std::cout << '\n'; } } void push_back(Matrix const& m) { if (count < max) { stack[std::slice(count * dim * dim, dim * dim, 1)] = m.data[std::slice(0, dim * dim, 1)]; ++count; } } }; int main() { constexpr int dim = 3; Matrix m{dim}; MatrixStack<dim,12> stack; m.identity(); stack.push_back(m); m.clear(1); m.secondary_diagonal() = 3; stack.push_back(m); for (int i = 0; i != dim<span class="sy4
参见
|
应用广义切片后对 valarray 子集的代理
(类模板) |