Negate Function In C++ STL

In this article, we will discuss the negate function in C++ with its syntax and examples.

What is Negate function?

The negate function is used to change the sign of the values, or to negate the given values. Positive values are converted to negative values and vice versa. This class's objects can be utilized with common algorithms like transform.

Syntax:

It has the following syntax:

Example

transform(arr_begin, arr_end, arr2_begin, negate())

Parameters:

Four parameters are supported by it, and they are as follows:

arr_begin:

It is the specified array's lower bound.

arr_end:

It is the specified array's upper bound.

arr2_begin:

It is the second array's bottom bound , where the updated modified values are to be stored.

negate:

This function is used to negate the values that are provided in the array.

Values returned:

The identical values are returned but with the reverse sign.

In C++, transform can be used in two ways:

1. Unary Operation:

If you want to convert input into output, apply a unary operator.

Syntax:

It has the following syntax:

Example

transform(Iterator inputBegin, Iterator inputEnd,
Iterator OutputBegin, unary_operation)

Program:

Let us take an example to illustrate the negate function using unary operation in C++.

Example

#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:

Output

4 16 36 64 100

2. Binary Operation:

You can use a binary operator to transform input into output.

Syntax:

It has the following syntax:

Example

transform(Iterator inputBegin1, Iterator inputEnd1,
Iterator inputBegin2, Iterator OutputBegin,
binary_operation)

Program:

Let us take an example to illustrate the negate function using binary operation in C++.

Example

#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:

Output

Result after addition: 6 6 6 6 6

Now Below is the implementation that shows the working of negate function:

Program:

Example

#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:

Output

15 17 -10 -50 60

Input Required

This code uses input(). Please provide values below: