Function_Objects
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
Function_Objects - Objects with an operator() defined. Function
objects are used in place of pointers to functions as arguments to
templated algorithms.
SYNOPSIS
#include<functional>
// typedefs
template <class Arg, class Result>
struct unary_function;
template <class Arg1, class Arg2, class Result>
struct binary_function;
DESCRIPTION Function objects are objects with an operator() defined.
They are important for the effective use of the standard library's
generic algorithms, because the interface for each algorithmic
template can accept either an object with an operator() defined,
or a pointer to a function. The Standard C++ Library provides
both a standard set of function objects, and a pair of classes
that you can use as the base for creating your own function
objects.
Function objects that take one argument are called unary function
objects. Unary function objects are required to provide the
typedefs argument_type and result_type. Similarly, function
objects that take two arguments are called binary
function objects and, as such, are required to provide the typedefs
first_argument_type, second_argument_type, and result_type.
The classes unary_function and binary_function make the task of c
reating templated function objects easier. The necessary typedefs
for a unary or binary function object are provided by inheriting
from the appropriate function object class.
The function objects provided by the standard library are listed
below, together with a brief description of their operation. This
class reference also includes an alphabetic entry for each
function.
Name Operation
arithmetic functions
plus addition x + y
minus subtraction x - y
multiplies multiplication x * y
divides division x / y
modulus remainder x % y
negate negation - x
comparison functions
equal_to equality test x == y
not_equal_to inequality test x != y
greater greater comparison x > y
less less-than comparison x < y
greater_equal greater than or equal comparison x >= y
less_equal less than or equal comparison x <= y
logical functions
logical_and logical conjunction x && y
logical_or logical disjunction x || y
logical_not logical negation ! x
INTERFACE
template <class Arg, class Result>
struct unary_function{
typedef Arg argument_type;
typedef Result result_type;
};
template <class Arg1, class Arg2, class Result>
struct binary_function{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
// Arithmetic Operations
template<class T>
struct plus : binary_function<T, T, T> {
T operator() (const T&, const T&) const;
};
template <class T>
struct minus : binary_function<T, T, T> {
T operator() (const T&, const T&) const;
};
template <class T>
struct multiplies : binary_function<T, T, T> {
T operator() (const T&, const T&) const;
};
template <class T>
struct divides : binary_function<T, T, T> {
T operator() (const T&, const T&) const;
};
template <class T>
struct modulus : binary_function<T, T, T> {
T operator() (const T&, const T&) const;
};
template <class T>
struct negate : unary_function<T, T> {
T operator() (const T&) const;
};
// Comparisons
template <class T>
struct equal_to : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
template <class T>
struct not_equal_to : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
template <class T>
struct greater : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
template <class T>
struct less : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
template <class T>
struct greater_equal : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
template <class T>
struct less_equal : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
// Logical Comparisons
template <class T>
struct logical_and : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
template <class T>
struct logical_or : binary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
template <class T>
struct logical_not : unary_function<T, T, bool> {
bool operator() (const T&, const T&) const;
};
EXAMPLE
//
// funct_ob.cpp
//
#include<functional>
#include<deque>
#include<vector>
#include<algorithm>
#include <iostream.h>
//Create a new function object from unary_function
template<class Arg>
class factorial : public unary_function<Arg, Arg>
{
public:
Arg operator()(const Arg& arg)
{
Arg a = 1;
for(Arg i = 2; i <= arg; i++)
a *= i;
return a;
}
};
int main()
{
//Initialize a deque with an array of ints
int init[7] = {1,2,3,4,5,6,7};
deque<int> d(init, init+7);
//Create an empty vector to store the factorials
vector<int> v((size_t)7);
//Transform the numbers in the deque to their factorials and
// store in the vector
transform(d.begin(), d.end(), v.begin(), factorial<int>());
//Print the results
cout << "The following numbers: " << endl << " ";
copy(d.begin(),d.end(),ostream_iterator<int,char>(cout," "));
cout << endl << endl;
cout << "Have the factorials: " << endl << " ";
copy(v.begin(),v.end(),ostream_iterator<int,char>(cout," "));
return 0;
}
Output :
The following numbers:
1 2 3 4 5 6 7
Have the factorials:
1 2 6 24 120 720 5040
WARNINGS
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you'll have to write :
vector<int, allocator<int> > and deque<int, allocator<int> > instead
of :
vector<int> and deque<int>
SEE ALSO
binary_function, unary_function
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
divides
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
divides - Returns the result of dividing its first argument by its
second.
SYNOPSIS
#include <functional>
template <class T>
struct divides;
DESCRIPTION
divides is a binary function object. Its operator() returns the
result of dividing x by y. You can pass a divides object to
any algorithm that requires a binary function. For example, the
transform algorithm applies a binary operation to corresponding
values in two collections and stores the result. divides would be
used in that algorithm in the following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(), vecResult.begin(),
divides<int>());
After this call to transform, vecResult[n] will contain vec1[n] divided
by vec2[n].
INTERFACE
template <class T>
struct divides : binary_function<T, T, T>
{
typedef typename binary_function<T, T, T>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, T>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, T>::result_type
result_type;
T operator() (const T&, const T&) const;
};
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
less
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
less - Binary function object that returns true if its first
argument is less than its second
SYNOPSIS
#include<functional>
template <class T>
struct less : public binary_function<T, T, bool> ;
DESCRIPTION
less is a binary function object. Its operator() returns true if x
is less than y. You can pass a less object to any algorithm
that requires a binary function. For example, the transform
algorithm applies a binary operation to corresponding values in two
collections and stores the result of the function. less
would be used in that algorithm in the following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(),
vecResult.begin(), less<int>());
After this call to transform, vecResult(n) will contain a "1" if
vec1(n) was less than vec2(n) or a "0" if vec1(n) was greater than
or equal to vec2(n).
INTERFACE
template <class T>
struct less : binary_function<T, T, bool> {
typedef typename binary_function<T, T, bool>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, bool>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, bool>::result_type
result_type;
bool operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you'll have to write :
vector<int, allocator<int> >
instead of
vector<int>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
less_equal
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
less_equal - Binary function object that returns true if its first
argument is less than or equal to its second
SYNOPSIS
#include<functional>
template <class T>
struct less_equal : public binary_function<T, T, bool>;
DESCRIPTION
less_equal is a binary function object. Its operator() returns true
if x is less than or equal to y. You can pass a less_equal
object to any algorithm that requires a binary
function. For example, the sort algorithm can accept a binary
function as an alternate comparison object to sort a
sequence. less_equal would be used in that algorithm in the
following manner:
vector<int> vec1;
sort(vec1.begin(), vec1.end(),less_equal<int>());
After this call to sort, vec1 will be sorted in ascending order.
INTERFACE
template <class T>
struct less_equal : binary_function<T, T, bool> {
typedef typename binary_function<T, T, bool>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, bool>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, bool>::result_type
result_type;
bool operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you'll have to write :
vector<int, allocator<int> >
instead of
vector<int>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
logical_and
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
logical_and - Binary function object that returns true if both of
its arguments are true.
SYNOPSIS
#include <functional>
template <class T>
struct logical_and : public binary_function<T, T, bool>;
DESCRIPTION
logical_and is a binary function object. Its operator() returns
true if both x and y are true. You can pass a logical_and object
to any algorithm that requires a binary function. For
example, the transform algorithm applies a binary operation to
corresponding values in two collections and stores the result
of the function. logical_and is used in that algorithm in the
following manner:
vector<bool> vec1;
vector<bool> vec2;
vector<bool> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(),
vecResult.begin(), logical_and<bool>());
After this call to transform, vecResult(n) will contain a "1" (true)
if both vec1(n) and vec2(n) are true or a "0" (false) if either
vec1(n) or vec2(n) is false.
INTERFACE
template <class T>
struct logical_and : binary_function<T, T, bool> {
typedef typename binary_function<T, T, bool>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, bool>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, bool>::result_type
result_type;
bool operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, you
will need to always supply the Allocator template argument. For
instance, you will have to write :
vector<bool, allocator<bool> >
instead of:
vector<bool>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
logical_not
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
logical_not - Unary function object that returns true if its
argument is false.
SYNOPSIS
#include <functional>
template <class T>
struct logical_not : unary_function<T, bool> ;
DESCRIPTION
logical_not is a unary function object. Its operator() returns true
if its argument is false. You can pass a logical_not object to any
algorithm that requires a unary function. For example, the
replace_if algorithm replaces an element with another value if the
result of a unary operation is true. logical_not is used in that
algorithm in the following manner:
vector<int> vec1;
void replace_if(vec1.begin(), vec1.end(),
logical_not<int>(),1);
This call to replace_if replaces all zeros in the vec1 with "1".
INTERFACE
template <class T>
struct logical_not : unary_function<T, bool> {
typedef typename unary_function<T, bool>::argument_type
argument_type;
typedef typename unary_function<T, bool>::result_type result_type;
bool operator() (const T&) const;
};
WARNING
If your compiler does not support default template parameters, you
will need to always supply the Allocator template argument. For
instance, you will have to write :
vector<int, allocator<int> >
instead of :
vector<int>
SEE ALSO
function objects, unary_function
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
logical_or
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
logical_or - Binary function object that returns true if either of
its arguments are true.
SYNOPSIS
#include <functional>
template <class T>
struct logical_or : binary_function<T, T, bool> ;
DESCRIPTION
logical_or is a binary function object. Its operator() returns true
if either x or y are true. You can pass a logical_or object to
any algorithm that requires a binary function. For example, the
transform algorithm applies a binary operation to corresponding
values in two collections and stores the result of the function.
logical_or is used in that algorithm in the following manner:
vector<bool> vec1;
vector<bool> vec2;
vector<bool> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(),
vecResult.begin(), logical_or<bool>());
After this call to transform, vecResult(n) will contain a "1" (true)
if either vec1(n) or vec2(n) is true or a "0" (false) if both
vec1(n) and vec2(n) are false.
INTERFACE
template <class T>
struct logical_or : binary_function<T, T, bool> {
typedef typename binary_function<T, T, bool>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, bool>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, bool>::result_type
result_type;
bool operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, you
will need to always supply the Allocator template argument. For
instance, you will have to write :
vector<bool, allocator<bool> >
instead of:
vector<bool>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
minus
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
minus - Returns the result of subtracting its second argument from
its first.
SYNOPSIS
#include<functional>
template <class T>
struct minus : public binary_function<T, T, T>;
DESCRIPTION
minus is a binary function object. Its operator() returns the
result of x minus y. You can pass a minus object to any
algorithm that requires a binary function. For example, the
transform algorithm applies a binary operation to corresponding
values in two collections and stores the result. minus would
be used in that algorithm in the following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(),
vecResult.begin(), minus<int>());
After this call to transform, vecResult(n) will contain vec1(n) minus
vec2(n).
INTERFACE
template <class T>
struct minus : binary_function<T, T, T> {
typedef typename binary_function<T, T, T>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, T>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, T>::result_type result_type;
T operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you will have to write :
vector<int, allocator<int> >
instead of :
vector<int>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
modulus
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
modulus - Returns the remainder obtained by dividing the first
argument by the second argument.
SYNOPSIS
#include<functional>
template <class T>
struct modulus : public binary_function<T, T, T> ;
DESCRIPTION
modulus is a binary function object. Its operator() returns the
remainder resulting from of x divided by y. You can pass a modulus
object to any algorithm that requires a binary function. For
example, the transform algorithm applies a binary operation to
corresponding values in two collections and stores the result.
modulus would be used in that algorithm in the
following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(),
vecResult.begin(), modulus<int>());
After this call to transform, vecResult(n) will contain the remainder
of vec1(n) divided by vec2(n).
INTERFACE
template <class T>
struct modulus : binary_function<T, T, T> {
typedef typename binary_function<T, T, T>::second_argument_type
n second_argument_type;
typedef typename binary_function<T, T, T>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, T>::result_type result_type;
T operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you will need to write :
vector<int, allocator<int> >
instead of
vector<int>
SEE ALSO
binary_function, function object
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
multiplies
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
multiplies - A binary function object that returns the result of
multiplying its first and second arguments.
SYNOPSIS
#include<functional>
template <class T>
struct multiplies : binary_function<T, T, T> {
typedef typename binary_function<T, T, T>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, T>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, T>::result_type result_type;
T operator() (const T&, const T&) const;
};
DESCRIPTION
multiplies is a binary function object. Its operator() returns the
result of multiplying x and y. You can pass a multiplies object to
any algorithm that uses a binary function. For example, the
transform algorithm applies a binary operation to corresponding
values in two collections and stores the result. multiplies
would be used in that algorithm in the following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(), vec2.end(),
vecResult.begin(), multiplies<int>());
After this call to transform, vecResult(n) will contain vec1(n)
times vec2(n).
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you will have to write :
vector<int, allocator<int> >
instead of :
vector<int>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
negate
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
negate - Unary function object that returns the negation of its
argument.
SYNOPSIS
#include <functional>
template <class T>
struct negate : public unary_function<T, T>;
DESCRIPTION
negate is a unary function object. Its operator() returns the
negation of its argument, i.e., true if its argument is false,
or false if its argument is true. You can pass a negate
object to any algorithm that requires a unary function. For
example, the transform algorithm applies a unary operation to the
values in a collection and stores the result. negate could be
used in that algorithm in the following manner:
vector<int> vec1;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vecResult.begin(), negate<int>());
After this call to transform, vecResult(n) will contain the negation
of the element in vec1(n).
INTERFACE
template <class T>
struct negate : unary_function<T, T> {
typedef typename unary_function<T,T>::argument_type argument_type;
typedef typename unary_function<T,T>::result_type result_type;
T operator() (const T&) const;
};
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you will need to write :
vector<int, allocator<int> >
instead of :
vector<int>
SEE ALSO
function objects, unary_function
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
Negators
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
Negators - Function adaptors and function objects used to reverse
the sense of predicate function objects.
SYNOPSIS
#include <functional>
template <class Predicate>
class unary_negate;
template <class Predicate>
unary_negate<Predicate> not1(const Predicate&);
template <class Predicate>
class binary_negate;
template <class Predicate>
binary_negate<Predicate> not2(const Predicate&);
DESCRIPTION
Negators not1 and not2 are functions that take predicate function
objects as arguments and return predicate function objects with the
opposite sense. Negators work only with function objects
defined as subclasses of the classes unary_function and
binary_function. not1 accepts and returns unary predicate
function objects. not2 accepts and returns binary predicate
function objects.
unary_negate and binary_negate are function object classes that
provide return types for the negators, not1 and not2.
INTERFACE
template <class Predicate>
class unary_negate
: public unary_function<typename Predicate::argument_type, bool> {
public:
typedef typename unary_function<typename Predicate::argument_type,
bool>::argument_type argument_type;
typedef typename unary_function<typename Predicate::argument_type,
bool>::result_type result_type;
explicit unary_negate (const Predicate&);
bool operator() (const argument_type&) const;
};
template<class Predicate>
unary_negate <Predicate> not1 (const Predicate&);
template<class Predicate>
class binary_negate
: public binary_function<typename Predicate::first_argument_type,
typename Predicate::second_argument_type,
bool>
{
public:
typedef typename binary_function<typename
Predicate::first_argument_type,
typename Predicate::second_argument_type,
bool>::second_argument_type second_argument_type;
typedef typename binary_function<typename
Predicate::first_argument_type,
typename Predicate::second_argument_type,
bool>::first_argument_type first_argument_type;
typedef typename binary_function<typename
Predicate::first_argument_type,
typename Predicate::second_argument_type, bool>::result_type
result_type;
explicit binary_negate (const Predicate&);
bool operator() (const first_argument_type&,
const second_argument_type&) const;
};
template <class Predicate>
binary_negate<Predicate> not2 (const Predicate&);
EXAMPLE
//
// negator.cpp
//
#include<functional>
#include<algorithm>
#include <iostream.h>
//Create a new predicate from unary_function
template<class Arg>
class is_odd : public unary_function<Arg, bool>
{
public:
bool operator()(const Arg& arg1) const
{
return (arg1 % 2 ? true : false);
}
};
int main()
{
less<int> less_func;
// Use not2 on less
cout << (less_func(1,4) ? "TRUE" : "FALSE") << endl;
cout << (less_func(4,1) ? "TRUE" : "FALSE") << endl;
cout << (not2(less<int>())(1,4) ? "TRUE" : "FALSE")
<< endl;
cout << (not2(less<int>())(4,1) ? "TRUE" : "FALSE")
<< endl;
//Create an instance of our predicate
is_odd<int> odd;
// Use not1 on our user defined predicate
cout << (odd(1) ? "TRUE" : "FALSE") << endl;
cout << (odd(4) ? "TRUE" : "FALSE") << endl;
cout << (not1(odd)(1) ? "TRUE" : "FALSE") << endl;
cout << (not1(odd)(4) ? "TRUE" : "FALSE") << endl;
return 0;
}
Output :
TRUE
FALSE
FALSE
TRUE
TRUE
FALSE
FALSE
TRUE
SEE ALSO
algorithm, binary_function, function_object, unary_function
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
not1
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
not1 - Function adaptor used to reverse the sense of a unary
predicate function object.
SYNOPSIS
#include <functional>
template<class Predicate>
unary_negate <Predicate> not1 (const Predicate&);
DESCRIPTION
not1 is a function adaptor, known as a negator, that takes a unary
predicate function object as its argument and returns a unary
predicate function object that is the complement of the
original. unary_negate is a function object class that provides a
return type for the not1 negator.
Note that not1 works only with function objects that are defined as
subclasses of the class unary_function.
SEE ALSO
negators, not2, unary_function, unary_negate,
pointer_to_unary_function
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
not2
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
not2 - Function adaptor used to reverse the sense of a binary
predicate function object.
SYNOPSIS
#include <functional>
template <class Predicate>
binary_negate<Predicate> not2 (const Predicate& pred);
DESCRIPTION
not2 is a function adaptor, known as a negator, that takes a binary
predicate function object as its argument and returns a binary
predicate function object that is the complement of the original.
binary_negate is a function object class that provides a return
type for the not2 negator.
Note that not2 works only with function objects that are defined as
subclasses of the class binary_function.
SEE ALSO
binary_function, binary_negate, negators, not1,
pointer_to_binary_function, unary_negate
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
not_equal_to
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
not_equal_to - Binary function object that returns true if its
first argument is not equal to its second.
SYNOPSIS
#include <functional>
template <class T>
struct not_equal_to : public binary_function<T, T, bool> ;
DESCRIPTION
not_equal_to is a binary function object. Its operator() returns
true if x is not equal to y. You can pass a not_equal_to object to
any algorithm that requires a binary function. For example, the
transform algorithm applies a binary operation to corresponding
values in two collections and stores the result. not_equal_to
would be used in that algorithm in the following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(),
vecResult.begin(), not_equal_to<int>());
After this call to transform, vecResult(n) will contain a "1" if
vec1(n) was not equal to vec2(n) or a "1" if vec1(n) was equal to
vec2(n).
INTERFACE
template <class T>
struct not_equal_to : binary_function<T, T, bool> {
typedef typename binary_function<T, T, bool>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, bool>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, bool>::result_type
result_type;
bool operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you will need to write :
vector<int, allocator<int> >
instead of :
vector<int>
SEE ALSO
binary_function, function object
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
plus
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
plus - A binary function object that returns the result of adding
its first and second arguments.
SYNOPSIS
#include <functional>
template<class T>
struct plus : public binary_function<T, T, T> ;
DESCRIPTION
plus is a binary function object. Its operator() returns the result
of adding x and y. You can pass a plus object to any algorithm
that uses a binary function. For example, the transform
algorithm applies a binary operation to corresponding values in two
collections and stores the result. plus would be used in that
algorithm in the following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(),
vec2.begin(),
vecResult.begin(), plus<int>());
After this call to transform, vecResult(n) will contain vec1(n) plus
vec2(n).
INTERFACE
template<class T>
struct plus : binary_function<T, T, T> {
typedef typename binary_function<T, T, T>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, T>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, T>::result_type result_type;
T operator() (const T&, const T&) const;
};
WARNING
If your compiler does not support default template parameters, you
need to always supply the Allocator template argument. For
instance, you will need to write:
vector<int, allocator<int> >
instead of :
vector<int>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
Predicates
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
Predicates - A function or a function object that returns a boolean
(true/false) value or an integer value.
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
times
Standard C++ Library
NAME
times - A binary function object that returns the result of multiplying
its first and second arguments.
SYNOPSIS
#include<functional>
template <class T>
struct times : binary_function<T, T, T> {
typedef typename binary_function<T, T, T>::second_argument_type
second_argument_type;
typedef typename binary_function<T, T, T>::first_argument_type
first_argument_type;
typedef typename binary_function<T, T, T>::result_type result_type;
T operator() (const T&, const T&) const;
};
DESCRIPTION
times is a binary function object. Its operator() returns the
result of multiplying x and y. You can pass a times object to any
algorithm that uses a binary function. For example, the transform
algorithm applies a binary operation to corresponding values in two
collections and stores the result. times would be used in that
algorithm in the following manner:
vector<int> vec1;
vector<int> vec2;
vector<int> vecResult;
transform(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(),
vecResult.begin(), times<int>());
After this call to transform, vecResult(n) will contain vec1(n) times
vec2(n).
WARNING
If your compiler does not support default template parameters, then
you need to always supply the Allocator template argument. For
instance, you will have to write :
vector<int, allocator>
instead of :
vector<int>
SEE ALSO
binary_function, function objects
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
unary_function
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
unary_function - Base class for creating unary function objects.
SYNOPSIS
#include <functional>
template <class Arg, class Result>
struct unary_function{
typedef Arg argument_type;
typedef Result result_type;
};
DESCRIPTION
Function objects are objects with an operator() defined. They are
important for the effective use of the standard library's generic
algorithms, because the interface for each algorithmic template can
accept either an object with an operator() defined or a pointer to a
function. The standard library provides both a standard set of
function objects, and a pair of classes that you can use as the
base for creating your own function objects.
Function objects that take one argument are called unary function
objects. Unary function objects are required to provide the
typedefs argument_type and result_type. The unary_function class
makes the task of creating templated unary function objects
easier by providing the necessary typedefs for a unary
function object. You can create your own unary function objects
by inheriting from unary_function.
SEE ALSO
function objects, and Function Objects Section in User's Guide.
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
unary_negate
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
unary_negate - Function object that returns the complement of the
result of its unary predicate
SYNOPSIS
#include<functional>
template <class Predicate>
class unary_negate : public unary_function<typename
Predicate::argument_type,
bool>;
DESCRIPTION
unary_negate is a function object class that provides a return type
for the function adapter not1. not1 is a function adapter, known as
a negator, that takes a unary predicate function object as its
argument and returns a unary predicate function object that is the
complement of the original.
Note that not1 works only with function objects that are defined as
subclasses of the class unary_function.
INTERFACE
template <class Predicate>
class unary_negate
: public unary_function<Predicate::argument_type, bool> {
typedef typename unary_function<typename
Predicate::argument_type,bool>::argument_type argument_type;
typedef typename unary_function<typename
Predicate::argument_type,bool>::result_type result_type;
public:
explicit unary_negate (const Predicate&);
bool operator() (const argument_type&) const;
};
template<class Predicate>
unary_negate <Predicate> not1 (const Predicate&);
CONSTRUCTOR
explicit
unary_negate(const Predicate& pred);
Construct a unary_negate object from predicate pred.
OPERATOR
bool
operator()(const argument_type& x) const;
Return the result of pred(x)
SEE ALSO
not1, not2, unary_function, binary_negate
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
binary_function
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
binary_function - Base class for creating binary function objects.
SYNOPSIS
#include <functional>
template <class Arg1, class Arg2, class Result>
struct binary_function{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
DESCRIPTION
Function objects are objects with an operator() defined. They are
important for the effective use of the standard library's generic
algorithms, because the interface for each algorithmic template
can accept either an object with an operator() defined or a pointer
to a function. The Standard C++ Library provides both a standard set
of function objects, and a pair of classes that you can use as the
base for creating your own function objects.
Function objects that take two arguments are called binary function
objects. Binary function objects are required to provide the
typedefs first_argument_type, second_argument_type, and result_type.
The binary_function class makes the task of creating templated
binary function objects easier by providing the necessary typedefs
for a binary function object. You can create your own binary
function objects by inheriting from binary_function.
SEE ALSO
function objects_ unary_function, the Function Objects section of
the User's Guide.
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
binary_negate
Standard C++ Library
Copyright 1996, Rogue Wave Software, Inc.
NAME
binary_negate - Function object that returns the complement of the
result of its binary predicate
SYNOPSIS
#include <functional>
template<class Predicate>
class binary_negate ;
DESCRIPTION
binary_negate is a function object class that provides a return type
for the function adaptor not2. not2 is a function adaptor, known
as a negator, that takes a binary predicate function object as its
argument and returns a binary predicate function object that is the
complement of the original.
Note that not2 works only with function objects that are defined as
subclasses of the class binary_function.
INTERFACE
template<class Predicate>
class binary_negate
: public binary_function<typename
predicate::first_argument_type,
typename
Predicate::second_argument_type,
bool>
{
public:
typedef typename binary_function<typename
Predicate::first_argument_type, typename
Predicate::second_argument_type, bool>::second_argument_type
second_argument_type;
typedef typename binary_function<typename
Predicate::first_argument_type, typename
Predicate::second_argument_type, bool>::first_argument_type
first_argument_type;
typedef typename binary_function<typename
Predicate::first_argument_type, typename
Predicate::second_argument_type, bool>::result_type
result_type;
explicit binary_negate (const Predicate&);
bool operator() (const first_argument_type&,
const second_argument_type&) const;
};
// Non-member Functions
template <class Predicate>
binary_negate<Predicate> not2 (const Predicate& pred);
CONSTRUCTOR
explicit binary_negate(const Predicate& pred);
Construct a binary_negate object from predicate pred.
OPERATOR
bool
operator()(const first_argument_type& x,
const second_argument_type& y) const;
Return the result of pred(x,y)
SEE ALSO
binary_function, not2, unary_negate
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee