Valarray is a class similar to vector but is efficient than vector if it comes to mathematical operations.
It provides many element-wise operations, various forms of generalized subscript operators, slicing and indirect access.
In some mathematical operations, valarrays are more efficient than vector operations.
Some of the APIs provided by valarray class are:
apply(the function that performs the operation on every element)
This API applies manipulation on the given arguments to all the array elements.
valarray<int> arr = {1,2,3,4,5,6};
arr.apply([] (int x){return x = x+ 5;});
The above code increments every element by 5
sum()
This API performs the sum of all elements in the given array
valarray<int> arr = {1,2,3,4,5};
arr.sum();
The above code sums up all the elements of the array which is 15.
min()
This API finds out the smallest element in the array.
max()
This API finds out the largest element in the array.
valarray<int> arr = {1,2,3,4,5};
arr.min();
arr.max();
First API call will return 1 and second API call will return 5
shift(amount of shift)
This API will shift elements by the given value of the amount of shift. If it is the positive value then perform and perform the left shift if it is negative.
valarray<int> arr = {1,2,3,4,5};
valarray<int> arr2;
valarray<int> arr3;
arr2 = arr.shift(2);
arr3 = arr.shift(-2);
First API call will return the array as {3,4,5,0,0}
Second API call will return the array as {0,0,1,2,3}
cshift(number of rotations)
This API performs right-side rotation if the value is negative and left-side rotation if the value is positive.
valarray<int> arr = {1,2,3,4,5};
valarray<int> arr2;
valarray<int> arr3;
arr2 = arr.shift(2);
arr3 = arr.shift(-2);
First API call will return the array as {3,4,5,1,2}
Second API call will return the array as {4,5,1,2,3}
swap()
This API performs swap operation on two valarrays.
valarray<int> arr1 = {1,2,3,4,5};
valarray<int> arr2 = {-1,-2,-3,-4,-5};
arr1.swap(arr2);
After the above operations, arr1 is {-1,-2,-3,-4,-5}
arr2 is {1,2,3,4,5}
It provides many element-wise operations, various forms of generalized subscript operators, slicing and indirect access.
In some mathematical operations, valarrays are more efficient than vector operations.
Some of the APIs provided by valarray class are:
apply(the function that performs the operation on every element)
This API applies manipulation on the given arguments to all the array elements.
valarray<int> arr = {1,2,3,4,5,6};
arr.apply([] (int x){return x = x+ 5;});
The above code increments every element by 5
sum()
This API performs the sum of all elements in the given array
valarray<int> arr = {1,2,3,4,5};
arr.sum();
The above code sums up all the elements of the array which is 15.
min()
This API finds out the smallest element in the array.
max()
This API finds out the largest element in the array.
valarray<int> arr = {1,2,3,4,5};
arr.min();
arr.max();
First API call will return 1 and second API call will return 5
shift(amount of shift)
This API will shift elements by the given value of the amount of shift. If it is the positive value then perform and perform the left shift if it is negative.
valarray<int> arr = {1,2,3,4,5};
valarray<int> arr2;
valarray<int> arr3;
arr2 = arr.shift(2);
arr3 = arr.shift(-2);
First API call will return the array as {3,4,5,0,0}
Second API call will return the array as {0,0,1,2,3}
cshift(number of rotations)
This API performs right-side rotation if the value is negative and left-side rotation if the value is positive.
valarray<int> arr = {1,2,3,4,5};
valarray<int> arr2;
valarray<int> arr3;
arr2 = arr.shift(2);
arr3 = arr.shift(-2);
First API call will return the array as {3,4,5,1,2}
Second API call will return the array as {4,5,1,2,3}
swap()
This API performs swap operation on two valarrays.
valarray<int> arr1 = {1,2,3,4,5};
valarray<int> arr2 = {-1,-2,-3,-4,-5};
arr1.swap(arr2);
After the above operations, arr1 is {-1,-2,-3,-4,-5}
arr2 is {1,2,3,4,5}
Comments
Post a Comment