std::plus in c++
It is a Function object for performing addition. The object class whose call returns the result of adding its two arguments (as returned by operator + ).
Syntax :
template struct plus : binary_function
{
T operator() (const T& x, const T& y) const { return x + y; }
};
Template parameters :
T - Type of the arguments and return type of the functional call.
The type shall support the operation (operator+).
Member types :
x : Type of the first argument in member operator()
y : Type of the second argument in member operator()
result_type : Type returned by member operator()
// C++ program to illustrate std::plus // by adding the respective elements of 2 arrays #include <iostream> // std::cout #include <functional> // std::plus #include <algorithm> // std::transform int main() { // First array int first[] = { 1, 2, 3, 4, 5 }; // Second array int second[] = { 10, 20, 30, 40, 50 }; // Result array int results[5]; // std::transform applies std::plus to the whole array std::transform(first, first + 5, second, results, std::plus<int>()); // Printing the result array for (int i = 0; i < 5; i++) std::cout << results[i] << " "; return 0; } |
chevron_right
filter_none
Output:
11 22 33 44 55
Another example:
// C++ program to illustrate std::plus // by adding all array elements with a number #include <bits/stdc++.h> int main() { // Array with elements to be added int arr[] = { 10, 20, 30 }; // size of array int size = sizeof(arr) / sizeof(arr[0]); // Variable with which array is to be added int num = 100; // Variable to store result int result; // using std::accumulate to perform addition on array with num // using std::plus result = std::accumulate(arr, arr + size, num, std::plus<int>()); // Printing the result std::cout << "The result of 100 + 10 + 20 + 30 is " << result; return 0; } |
chevron_right
filter_none
Output:
The result of 100 + 10 + 20 + 30 is 160
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples
- How to erase an element from a vector using erase() and reverse_iterator?
- iomanip setbase() function in C++ with Examples
- iomanip setpricision() function in C++ with Examples
- iomanip setiosflags() function in C++ with Examples
- iomanip setfill() function in C++ with Examples
- Manipulators in C++ with Examples
- iomanip setw() function in C++ with Examples
- iomanip resetiosflags() function in C++ with Examples
- Difference Between Constructor and Destructor in C++
- fill in C++ STL
- Conditional or Ternary Operator (?:) in C/C++



