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
를 생성합니다
(public member function) |
|
slice_array
를 소멸합니다
(public member function) |
|
|
내용을 할당합니다
(public member function) |
|
|
slice가 참조하는 배열에 대해 산술 연산을 수행합니다
(public member function) |
예제
이 코드 실행
#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; ++i) { m.clear(); m.row(i) = i + 1; stack.push_back(m); } for (int i = 0; i != dim; ++i) { m.clear(); m.column(i) = i + 1; stack.push_back(m); } m.clear(); m.row(1) = std::valarray<int>{4, 5, 6}; stack.push_back(m); m.clear(); m.column(1) = std::valarray<int>{7, 8, 9}; stack.push_back(m); stack.print_all(); }
출력:
1 0 0 │ 1 1 3 │ 1 1 1 │ 0 0 0 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 0 0 0 │ 0 7 0 │ 0 1 0 │ 1 3 1 │ 0 0 0 │ 2 2 2 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 4 5 6 │ 0 8 0 │ 0 0 1 │ 3 1 1 │ 0 0 0 │ 0 0 0 │ 3 3 3 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 0 0 0 │ 0 9 0 │
참고 항목
|
gslice를 적용한 후 valarray의 부분 집합에 대한 프록시
(클래스 템플릿) |