C++ <valarray> - operator- Function
The C++ valarray::operator- function is used to apply unary minus operator to each element of the valarray.
Syntax
valarray<T> operator-() const;
Parameters
No parameter is required.
Return Value
Returns a new valarray object with modified values.
Time Complexity
Depends on library implementation.
Example:
In the example below, the valarray::operator- function is used to perform unary minus operation on each element of the given valarray.
#include <iostream> #include <valarray> using namespace std; int main (){ valarray<int> varr1 {10, 20, 30, 40}; //result of unary minus operation //on varr1 is stored in varr2 valarray<int> varr2 = -varr1; cout<<"varr1 contains: "; for(int i = 0; i < varr1.size(); i++) cout<<varr1[i]<<" "; cout<<"\nvarr2 contains: "; for(int i = 0; i < varr2.size(); i++) cout<<varr2[i]<<" "; return 0; }
The output of the above code will be:
varr1 contains: 10 20 30 40 varr2 contains: -10 -20 -30 -40
❮ C++ <valarray> Library