Namespaces
Variants

std:: gslice_array

From cppreference.net
헤더에 정의됨 <valarray>
template < class T >
class gslice_array ;

std::gslice_array valarray 첨자 연산자 std:: gslice 인자와 함께 사용될 때 활용되는 헬퍼 템플릿입니다. 이는 std:: gslice 객체로 지정된 배열의 부분집합에 대한 참조 의미론을 가집니다.

목차

멤버 타입

타입 정의
value_type T

멤버 함수

gslice_array 를 생성합니다
(public member function)
gslice_array 를 소멸합니다
(public member function)
내용을 할당합니다
(public member function)
일반 슬라이스가 참조하는 배열에 대해 산술 연산을 수행합니다.
(public member function)

예제

#include <cassert>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <valarray>
int main()
{
    std::valarray<int> data(32);
    std::iota(std::begin(data), std::end(data), 0);
    const std::size_t offset = 1, z = 2, y = 3, x = 4;
    const std::valarray<std::size_t> sizes{z, y, x};
    const std::valarray<std::size_t> strides{15, 5, 1};
    const std::gslice gslice = std::gslice(offset, sizes, strides);
    // 인덱스는 다음 공식에 따라 생성됩니다:
    // index[k] = offset + [0,1,2)*15 + [0,1,2,3)*5 + [0,1,2,3,4)*1
    //          = offset + inner_product(sizes[k], strides);
    // 여기서 sizes[k] = {[0,z), [0,y), [0,x)}이며, 가장 오른쪽 인덱스(x)가
    // 가장 빠르게 증가합니다. 결과적으로 다음과 같은 인덱스 집합을 얻습니다:
    //  index[0]  = 1 + 0*15 + 0*5 + 0*1 = 1
    //  index[1]  = 1 + 0*15 + 0*5 + 1*1 = 2
    //  index[2]  = 1 + 0*15 + 0*5 + 2*1 = 3
    //  index[3]  = 1 + 0*15 + 0*5 + 3*1 = 4
    //  index[4]  = 1 + 0*15 + 1*5 + 0*1 = 6
    //  index[5]  = 1 + 0*15 + 1*5 + 1*1 = 7
    //  index[6]  = 1 + 0*15 + 1*5 + 2*1 = 8
    //  index[7]  = 1 + 0*15 + 1*5 + 3*1 = 9
    //  ...
    //  index[22] = 1 + 1*15 + 2*5 + 2*1 = 28
    //  index[23] = 1 + 1*15 + 2*5 + 3*1 = 29
    const std::valarray<int> indices = data[gslice];
    for (unsigned i = 0; i != indices.size(); ++i)
        std::cout << std::setfill('0') << std::setw(2) << indices[i] << ' ';
    std::cout << "\nTotal indices: " << indices.size() << '\n';
    assert(indices.size() == x * y * z);
    data = 0;
    std::gslice_array<int> gslice_array = data[gslice];
    gslice_array = 1;
    // 생성된 인덱스에 해당하는 셀 = '1', 건너뛴 셀 = '0'
    for (auto i : data)
        std::cout << i << ' ';
    std::cout << "\nSum of ones = " << data.sum() << '\n';
}

출력:

01 02 03 04 06 07 08 09 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29 
Total indices: 24
0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 
Sum of ones = 24

참고 항목

슬라이스를 적용한 후 valarray의 부분 집합에 대한 프록시
(클래스 템플릿)
(C++23)
다차원 비소유 배열 뷰
(클래스 템플릿)