Javatpoint Logo
Javatpoint Logo

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:

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:

Program:

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

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:

Program:

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

Output:

Result after addition: 6 6 6 6 6

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

Program:

Output:

15 17 -10 -50 60






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA