std::slice (Valarray slice selector)

Valarray slice selector : This class represents a valarray slice selector. It does not contain nor refers to any element – it only describes a selection of elements to be used as an index in valarray::operator[].

std::slice is the selector class that identifies a subset of std::valarray. An object of type std::slice holds three values: the starting index, the stride, and the total number of values in the subset. Objects of type std::slice can be used as indexes with valarray’s operator[].

class slice;

In simple terms it is used to slice based on an index. A valarray slice is defined by a starting index, a size, and a stride.Syntax :

slice( std::size_t start, std::size_t size, std::size_t stride );size_t star : is the index of the first element in the selection
size_t size : is the number of elements in the selection
stride : is the span that separates the elements selected.

In given Syntax, by default constructor isequivalent to slice(0, 0, 0). This constructor exists only to allow construction of arrays of slices.

It constructs a new slice with parameters start, size, stride. This slice will refer to size number of elements, each with the position:

Therefore, a slice with a stride higher than 1 does not select contiguous elements in the valarray. For example, slice(3, 4, 5) selects the elements 3, 8, 13 and 18.

// C++ program to test the functioning of std::slice

#include <iostream> // std::cout

#include <cstddef> // std::size_t

#include <valarray> // std::valarray, std::slice

intmain()

{

std::valarray<int> sample(12);

// initialising valarray

for(inti = 0; i < 13; ++i)

sample[i] = i;

// using slice from start 1 and size 3 and stride 4

std::valarray<int> bar = sample[std::slice(2, 3, 4)];

// display slice result

std::cout << "slice(2, 3, 4):";

for(std::size_tn = 0; n < bar.size(); n++)

std::cout << ' '<< bar[n];

std::cout << '\n';

return0;

}

Output:

slice(2, 3, 4): 2 6 10

Application : A simple application of slice is finding the trace of a matrix.

// C++ program to find trace of a matrix by using std::slice

#include <iostream> // std::cout

#include <valarray> // std::valarray, std::slice

usingnamespacestd;

intmain()

{

// row and column of matrix

introw = 3, col = 3;

// matrix of size row*col in row major form.

std::valarray<int> matrix(row * col);

// initialising matrix

for(inti = 0; i < row * col; ++i)

matrix[i] = i + 1;

// using slice from start 0 with size as col and stride col+1

std::valarray<int> diagonal = matrix[std::slice(0, col, col + 1)];

// finding trace using diagonal we got using slice

intindex = 0;

for(inti = 0; i < row; i++) {

for(intj = 0; j < col; j++)

std::cout << matrix[index++] << " "; // same as matrix[i][j]

std::cout << endl;

}

intsum = 0; // initialising sum as 0

// calculating trace of matrix

for(inti = 0; i < diagonal.size(); i++)

sum += diagonal[i];

std::cout << "Trace of matrix is : ";

std::cout << sum << endl; // sum is trace of matrix

return0;

}

Output:

1 2 3
4 5 6
7 8 9
Trace of matrix is : 15

This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.