In this post, we will explore the negate function in C++ along with its syntax and sample illustrations.
What is Negate function?
The negate method is employed to alter the polarity of the values, or to reverse the provided values. Positive values are transformed into negative values and vice versa. Instances of this class can be applied in conjunction with standard algorithms such as transform.
Syntax:
It has the following syntax:
transform(arr_begin, arr_end, arr2_begin, negate())
Parameters:
It supports four parameters, which include the following:
arr_begin:
It is the specified array's lower bound.
arr_end:
It is the specified array's upper bound.
arr2_begin:
It represents the lower boundary of the second array, indicating where the newly adjusted values will be saved.
negate:
This function is employed to invert the values given in the array.
Values returned:
The same values are produced, albeit with the opposite sign.
In C++, transform can be used in two ways:
1. Unary Operation:
If you wish to transform the given input into the desired output, you can achieve this by applying a single unary operator.
Syntax:
It has the following syntax:
transform(Iterator inputBegin, Iterator inputEnd,
Iterator OutputBegin, unary_operation)
Program:
Let's consider an example to demonstrate the usage of the negate function with unary operations in C++.
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int square(int x) { return x * x; }
int main()
{
int arr[] = {2, 4, 6, 8, 10};
int n = sizeof(arr) / sizeof(arr[0]);
// Apply square function to all elements of
// arr[] and store the modified elements
// back in arr[]
transform(arr, arr + n, arr, square);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Output:
4 16 36 64 100
2. Binary Operation:
You have the option to utilize a binary operator to convert the input into the desired output.
Syntax:
It has the following syntax:
transform(Iterator inputBegin1, Iterator inputEnd1,
Iterator inputBegin2, Iterator OutputBegin,
binary_operation)
Program:
Let's consider a scenario to demonstrate the negate function through binary manipulation in C++.
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
int main()
{
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {5, 4, 3, 2, 1};
vector<int> result(5); // Vector to store the result
// Binary operation: Add corresponding elements of arr1 and arr2
transform(arr1, arr1 + 5, arr2, result.begin(), plus<int>());
// Display the result
cout << "Result after addition: ";
for (int i : result)
cout << i << " ";
return 0;
}
Output:
Result after addition: 6 6 6 6 6
Now presented is the code snippet demonstrating the functionality of the negate function:
Program:
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main()
{
int arr[] = {5, 7, -20, -60, 50};
int constantToAdd = 10;
// using transform to add a constant value to each element
transform(arr, arr + 5, arr, bind(plus<int>(), placeholders::_1, constantToAdd));
for (int i = 0; i < 5; i++)
cout << arr[i] << ' ';
return 0;
}
Output:
15 17 -10 -50 60