C++98 introduced a special container called valarray to hold and provide mathematical operations on arrays efficiently.
- It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.
- As compare to vectors, valarrays are efficient in certain mathematical operations than vectors also.
Public member functions in valarray class :
1. apply() :- This function applies the manipulation given in its arguments to all the valarray elements at once and returns a new valarray with manipulated values.
2. sum() :- This function returns the summation of all the elements of valarrays at once.
The new valarray with manipulated values is : 15 7 25 6 35 The sum of old valarray is : 63 The sum of new valarray is : 88
3. min() :- This function returns the smallest element of valarray.
4. max() :- This function returns the largest element of valarray.
The largest element of valarray is : 30 The smallest element of valarray is : 1
5. shift() :- This function returns the new valarray after shifting elements by the number mentioned in its argument. If the number is positive, left-shift is applied, if number is negative, right-shift is applied.
6. cshift() :- This function returns the new valarray after circularly shifting(rotating) elements by the number mentioned in its argument. If the number is positive, left-circular shift is applied, if number is negative, right-circular shift is applied.
The new valarray after shifting is : 20 1 30 0 0 The new valarray after circular shifting is : 20 1 30 10 2
7. swap() :- This function swaps one valarray with other.
The contents of 1st valarray before swapping are : 1 2 3 4 The contents of 2nd valarray before swapping are : 2 4 6 8 The contents of 1st valarray after swapping are : 2 4 6 8 The contents of 2nd valarray after swapping are : 1 2 3 4
This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- std::fixed, std::scientific, std::hexfloat, std::defaultfloat in C++
- std::slice (Valarray slice selector)
- std::gslice | Valarray generalized slice selector
- std::setbase, std::setw , std::setfill in C++
- std::oct , std::dec and std::hex in C++
- std::stod, std::stof, std::stold in C++
- std::legendre, std::legendref and std::legendrel functions in C++17
- valarray sum() in C++
- valarray apply() in C++
- valarray min() in C++
- valarray shift() in C++
- max() function for valarray in C++
- cshift() function for valarray in C++
- valarray size() function in C++
- valarray swap() function in c++
- valarray resize() function in C++
- valarray begin() function in C++
- valarray end() function in C++
- valarray abs() function in C++
- valarray log10() function in C++