valarray apply() in C++ Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The apply() function is defined in valarray header file. This function returns a valarray with each of its elements initialized to the result of applying func to its corresponding element in *this. Syntax: valarray apply (T func(T)) const; valarray apply (T func(const T&)) const; Parameter: This method accepts a mandatory parameter func which represents the pointer to the function taking an argument of type T. Return Value: This method returns a valarray object with the results of applying func to all the elements of *this. Below programs illustrate the above function: Example 1:- CPP // C++ program to demonstrate // example of apply() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing valarray valarray<int> varr = { 15, 10, 30, 33, 40 }; // Declaring new valarray valarray<int> varr1; // Using apply() to increment all elements by 5 varr1 = varr.apply([](int x) { return x = x + 5; }); // Displaying new elements value cout << "The new valarray " << "with manipulated values is : "; for (int& x : varr1) cout << x << " "; cout << endl; return 0; } Output: The new valarray with manipulated values is : 20 15 35 38 45 Example 2:- CPP // C++ program to demonstrate // example of apply() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing valarray valarray<int> varr = { 15, 10, 30, 33, 40 }; // Declaring new valarray valarray<int> varr1; // Using apply() to decrement all elements by 5 varr1 = varr.apply([](int x) { return x = x - 5; }); // Displaying new elements value cout << "The new valarray" << " with manipulated values is : "; for (int& x : varr1) cout << x << " "; cout << endl; return 0; } Output: The new valarray with manipulated values is : 10 5 25 28 35 Comment More infoAdvertise with us Next Article valarray exp() function in C++ B bansal_rtk_ Follow Improve Article Tags : Misc C++ cpp-template cpp-valarray Practice Tags : CPPMisc Similar Reads std:: valarray class in C++ 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 5 min read valarray abs() function in C++ The abs() function is defined in valarray header file. This function is used to calculate the absolute value of each element in the valarray and returns a valarray containing the absolute values of all the elements. Syntax: abs(varr); Parameter: This function takes a mandatory parameter varr which r 2 min read valarray exp() function in C++ The exp() function is defined in valarray header file. This function is used to calculate e raised to the power equal to the value of the element in valarray. Syntax: exp(varr); Parameter: This function takes a mandatory parameter varr which represents valarray. Returns: This function returns a vala 2 min read valarray acos() function in C++ The acos() function is defined in valarray header file. This function is used to calculate arc cosine of the value of each element in valarray and returns a valarray containing the arc cosine of all the elements. Syntax: acos(varr); Parameter: This function takes a mandatory parameter varr which rep 2 min read valarray atan() function in C++ The atan() function is defined in valarray header file. This function is used to calculate arc tangent of the value of each element in valarray and returns a valarray containing the arc tangent of all the elements. Syntax: atan(varr); Parameter: This function takes a mandatory parameter varr which r 2 min read valarray asin() function in C++ The asin() function is defined in valarray header file. This function is used to calculate arc sine of the value of each element in valarray and returns a valarray containing the arc sine of all the elements. Syntax: asin(varr); Parameter: This function takes a mandatory parameter varr which represe 2 min read Like