cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : functional : plus
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
functional
iterator
memory
utility
functional
binary_function
unary_function
operator classes:
· divides
· equal_to
· greater
· greater_equal
· less
· less_equal
· logical_and
· logical_not
· logical_or
· minus
· modulus
· multiplies
· negate
· not_equal_to
· plus
adaptor functions:
· bind1st
· bind2nd
· mem_fun
· mem_fun_ref
· not1
· not2
· ptr_fun
types:
· binary_negate
· binder1st
· binder2nd
· const_mem_fun1_ref_t
· const_mem_fun1_t
· const_mem_fun_ref_t
· const_mem_fun_t
· mem_fun1_ref_t
· mem_fun1_t
· mem_fun_ref_t
· mem_fun_t
· pointer_to_binary_funct...
· pointer_to_unary_function
· unary_negate

-

plus class template
template <class T> struct plus;
<functional>

Addition function object class

This class defines function objects for the addition arithmetic operation.

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore it can be used in templates instead of a pointer to a function.

plus has its operator() member defined such that it returns the addition of its two arguments.

This class is derived from binary_function and is defined as:

template <class T> struct plus : binary_function <T,T,T> {
  T operator() (const T& x, const T& y) const
    {return x+y;}
};

Objects of this class can be used with some standard algorithms such as transform or accumulate.

Members

T operator() (const T& x, const T& y)
Member function returning the sum of its two arguments (x+y).

Example

// plus example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  int first[]={1,2,3,4,5};
  int second[]={10,20,30,40,50};
  int results[5];
  transform ( first, first+5, second, results, plus<int>() );
  for (int i=0; i<5; i++)
	  cout << results[i] << " ";
  cout << endl;
  return 0;
}

Output:


11 22 33 44 55

See also

minus Subtraction function object class (class template)
multiplies Multiplication function object class (class template)
divides Division function object class (class template)
modulus Mudulus function object class (class template)
negate Negative function object class (class template)
equal_to Function object class for equality comparison (class template)
binary_function Binary function object base class (class template)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us