negate function in C++ STL
Last Updated :
18 Jul, 2018
This function is used to negate the given values i.e. to change the sign of the values. It changes the positive values to negative and vice-versa.
Note: Objects of this class can be used on standard algorithms such as transform.
Syntax:
transform(arr_begin, arr_end, arr2_begin, negate())
Parameters: It accepts four parameters which are described below:
- arr_begin: It is the lower bound of the given array.
- arr_end: It is the upper bound of the given array.
-
arr2_begin: It is the lower bound of the second array in which the modified values to be updated.
- negate(): It is the function that is used to negate the values of the given array.
Return values: It returns the same values with the opposite sign.
Below is the implementation that shows the working of negate() function:
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 5, 7, -20, -60, 50 };
transform(arr, arr + 5, arr, negate< int >());
for ( int i = 0; i < 5; i++)
cout << arr[i] << ' ' ;
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...