Valarray slice selector in C++

In this tutorial, you will learn Valarray slice selector in C++. Here you will find:

  • inbuilt function
  • own user-defined function

Valarray slice selector is class defined in the inbuilt library known as Valarray. We can include that library in our program using the text shown in inverted comma ‘#include<valarray>’ at the beginning of the program.
This class is named as std:: slice and has three parameters passed to it; first indicates the index of the first element in the slice from where we are going to slice the data, the second parameter indicates the number of elements we want in the slice and the third parameter tells us the leap or jump between those elements.
Talking more about this class we can say that it neither refers to any element nor contain those elements; rather it only set out the selection of elements to be used in valarray:: operator[].

Member functions

All the member functions are public member functions.

  1. constructor : a slice constructor.
  2. start()     : returns start value of the slice.
  3. size()      : returns size of the slice.
  4. stride()    : returns stride or jump between the elements.

Constructors used in the class:

  • slice() : Equivalent to slice(0,0,0). This is the default constructor.
  • slice( std::size_t start, std::size_t length, std::size_t jump ) : This slice will refer to ‘length’ number of elements, each with the position given by :
    start + 0*jump
    start + 1*jump
    .  .  .
    .  .  .
    start + (length-1)*jump
    Here size_t is an unsigned integral type and it is defined in the library named “cstddef”( This library can be included using text ‘#include<cstddef>’ at the beginning of the program ). We use size_t instead of unsigned int because unsigned int may fail for array indexing on64-bit systems when the value exceeds UNIT_MAX or if it depends on 32-bit modular arithmetic.
  • slice( const slice& other) : This constructor constructs a copy of ‘other’.

Parameters Passed:

  • start: Index of the first element.
  • length: length or number of elements in the slice.
  • jump: span between the two consecutive elements.
  • other: Another slice whose copy has to be made.

Working: Valarray slice selector in C++

The working of the class and its member functions can be seen through the following example:

#include <iostream>     // std::cout
#include <valarray>     // std::valarray, std::slice
#include <cstddef>      //std::size_t
int main ()
{
  std::valarray<int> ex1 (10); //creating an object of valarray of array nature
  for (int i=0; i<10; ++i) ex1[i]=i;//initializing
  std::slice SL (1,4,2); //passing the 3 parameters i.e. start,length and jump(or stride)
  std::valarray<int> s = ex1[SL];//creating another object of array nature to store the slice
  std::cout << "slice is starting at : " << SL.start() << "\n";//prints the starting index
  std::cout << "Size of the slice is : " << SL.size() << "\n";//prints the slice size
  std::cout << "slice has stride or jump of : " << SL.stride() << "\n";//prints the span value
  std::cout << "slice(1,4,2) has output as :";
  for (std::size_t n1=0; n1<s.size(); n1++)  //loop to print the slice elements
    std::cout << ' ' << s[n1];//printing the elements after slicing
  std::cout << '\n'; //'\n' is for taking the cursor to new line

  return 0;
}

The output of the following program is: 

slice is starting at : 1 
Size of the slice is : 4 
slice has stride or jump of : 2 
slice(1,4,2) has output as : 1 3 5 7

Explanation:

Slicing is done by taking a jump in the following manner:
taking the above example as an explanation we can see starting index=1, size of slice= 4 ; which means the number of slice elements is 4, jump between elements or stride is taken as 2. Now having passed these values the slice will refer to size number of elements and the position of each element is given by :
first element = start + 0*stride
second element = start + 1*stride
third element = start + 2*stride
….
….
last element = start + (size-1)*stride
So for this example the elements will be : 1 + (0*2) = 1, 1 + (1*2) = 3, 1 + (2*2)= 5 and 1 + (3*2)= 7

Building our own function of Slice:

This is a simple function and is not the exact code as of the inbuilt valarray slice selector class.

/*Note: This slice function is not exact same as the inbulit valarray slice selector class. 
 thorugh this function I have just shown that how slice function works internally. This function is only for better understanding */
#include<iostream> //std::cout, std::endl;
#include<cstddef>  //std::size_t
void ownSlice(size_t st, size_t len, size_t jump);/*Three unsigned int type variables to store starting index, length and 
                                                    span among elements respectively.*/
int main()
{
  int a[14];//array declaration
  for(int i=0;i<14;i++)
   a[i]=i;              //initialisation
   ownSlice(0,5,3);   //calling the ownSlice function
  return 0;
}
void ownSlice(std::size_t st, std::size_t len, std::size_t jump) //function that performs slice operation
{
std::cout << "slice is starting at : " << st << "\n"; //prints starting index
std::cout << "Size of the slice is : " << len << "\n"; //prints number of slice elements
std::cout << "slice has stride or jump of : " << jump << "\n"; //prints span between each element

std::cout<<"The output of the received parameters is : "; // prints the elements after slicing
 for(int k=0;k<len;k++)
  {
     std::cout<<(st+(k*jump))<<" "; //calculation of the slice element
  }
std::cout<<std::endl;
}

The output of the following program is: 

slice is starting at : 0 
Size of the slice is : 5 
slice has stride or jump of : 3 
The output of the received parameters is : 0 3 6 9 12

Also read: Set of vectors in C++

Leave a Reply

Your email address will not be published. Required fields are marked *