52942.fb2 Standard Template Library Programmers Guide - читать онлайн бесплатно полную версию книги . Страница 6

Standard Template Library Programmers Guide - читать онлайн бесплатно полную версию книги . Страница 6

Function Objects

Introduction

Category: functors

Component type: overview

Summary

A Function Object, or Functor (the two terms are synonymous) is simply any object that can be called as if it is a function. An ordinary function is a function object, and so is a function pointer; more generally, so is an object of a class that defines operator().

Description

The basic function object concepts are Generator, Unary Function, and Binary Function: these describe, respectively, objects that can be called as f(), f(x), and f(x,y). (This list could obviously be extended to ternary function and beyond, but, in practice, no STL algorithms require function objects of more than two arguments.) All other function object concepts defined by the STL are refinements of these three.

Function objects that return bool are an important special case. A Unary Function whose return type is bool is called a Predicate, and a Binary Function whose return type is bool is called a Binary Predicate.

There is an important distinction, but a somewhat subtle one, between function objects and adaptable function objects. [1] In general, a function object has restrictions on the type of its argument. The type restrictions need not be simple, though: operator() may be overloaded, or may be a member template, or both. Similarly, there need be no way for a program to determine what those restrictions are. An adaptable function object, however, does specify what the argument and return types are, and provides nested typedefs so that those types can be named and used in programs. If a type F0 is a model of Adaptable Generator, then it must define F0::result_type. Similarly, if F1 is a model of Adaptable Unary Function then it must define F1::argument_type and F1::result_type, and if F2 is a model of Adaptable Binary Function then it must define F2::first_argument_type, F2::second_argument_type, and F2::result_type. The STL provides base classes unary_function and binary_function to simplify the definition of Adaptable Unary Functions and Adaptable Binary Functions. [2]

Adaptable function objects are important because they can be used by function object adaptors: function objects that transform or manipulate other function objects. The STL provides many different function object adaptors, including unary_negate (which returns the logical complement of the value returned by a particular AdaptablePredicate), and unary_compose and binary_compose, which perform composition of function object.

Finally, the STL includes many different predefined function objects, including arithmetic operations (plus, minus, multiplies, divides, modulus, and negate), comparisons (equal_to, not_equal_to, greater, less, greater_equal, and less_equal), and logical operations (logical_and, logical_or, and logical_not). It is possible to perform very sophisticated operations without actually writing a new function object, simply by combining predefined function objects and function object adaptors.

Examples

Fill a vector with random numbers. In this example, the function object is simply a function pointer.

vector<int> V(100);

generate(V.begin(), V.end(), rand);

Sort a vector of double by magnitude, i.e. ignoring the elements' signs. In this example, the function object is an object of a user-defined class.

struct less_mag : public binary_function<double, double, bool> {

 bool operator()(double x, double y) { return fabs(x) < fabs(y); }

};

vector<double> V;

sort(V.begin(), V.end(), less_mag());

Find the sum of elements in a vector. In this example, the function object is of a user-defined class that has local state.

struct adder : public unary_function<double, void> {

 adder() : sum(0) {}

 double sum;

 void operator()(double x) { sum += x; }

};

vector<double> V;

adder result = for_each(V.begin(), V.end(), adder()); [3]

cout << "The sum is " << result.sum << endl;

Remove all elements from a list that are greater than 100 and less than 1000.

list<int> L;

list<int>::iterator new_end = remove_if(L.begin(), L.end(), compose2(logical_and<bool>(), bind2nd(greater<int>(), 100), bind2nd(less<int>(), 1000)));

L.erase(new_end, L.end());

Concepts

• Generator

• Unary Function

• Binary Function

• Predicate

• Binary Predicate

• Adaptable Generator

• Adaptable Unary Function

• Adaptable Binary Function

• Adaptable Predicate

• Adaptable Binary Predicate

Types

• plus

• minus

• multiplies (formerly called times )

• divides

• modulus

• negate

• equal_to

• not_equal_to

• greater

• less

• greater_equal

• less_equal

• logical_and

• logical_or

• logical_not

• subtractive_rng

• identity

• project1st

• project2nd

• select1st

• select2nd

• unary_function

• binary_function

• unary_compose

• binary_compose

• unary_negate

• binary_negate

• binder1st

• binder2nd

• pointer_to_unary_function

• pointer_to_binary_function

Functions

• compose1

• compose2

• not1

• not2

• bind1st

• bind2nd

• ptr_fun

Notes

[1] The reason for the name "adaptable function object" is that adaptable function objects may be used by function object adaptors.

[2] The unary_function and binary_function bases are similar to the input_iterator, output_iterator, forward_iterator, bidirectional_iterator, and random_access_iterator bases: they are completely empty, and serve only to provide type information.

[3] This is an example of how to use function objects; it is not the recommended way of calculating the sum of elements in a vector. The accumulate algorithm is a better way of calculating a sum.

Concepts

Generator

Category: functors

Component type: concept

Description

A Generator is a kind of function object: an object that is called as if it were an ordinary C++ function. A Generator is called with no arguments.

Refinement of

Assignable

Associated types

Result typeThe type returned when the Generator is called

Notation

F A type that is a model of Generator

Result The result type of F

f Object of type F

Definitions

The range of a Generator is the set of all possible value that it may return.

Valid expressions

NameExpressionReturn type
Function callf()Result

Expression semantics

NameExpressionSemanticsPostcondition
Function callf()Returns some value of type Result [1]The return value is in f 's range.

Models

• Result (*)()

Notes

[1] Two different invocations of f may return different results: a Generator may refer to local state, perform I/O, and so on. The expression f() is permitted to change f's state; f might, for example, represent a pseudo-random number generator.

See also

Function Object overview, Unary Function, Binary Function, Adaptable Generator

Unary Function

Category: functors

Component type: concept

Description

A Unary Function is a kind of function object: an object that is called as if it were an ordinary C++ function. A Unary Function is called with a single argument.

Refinement of

Assignable

Associated types

Argument typeThe type of the Unary Function's argument.
Result typeThe type returned when the Unary Function is called

Notation

F A type that is a model of Unary Function

X The argument type of F

Result The result type of F

f Object of type F

x Object of type X

Definitions

The domain of a Unary Function is the set of all permissible values for its argument.

The range of a Unary Function is the set of all possible values that it may return.

Valid expressions

NameExpressionReturn type
Function callf(x)Result

Expression semantics

NameExpressionPreconditionSemanticsPostcondition
Function callf(x)x is in f's domainCalls f with x as an argument, and returns a value of type Result [1]The return value is in f's range

Models

• Result (*)(X)

Notes

[1] Two different invocations of f may return different results, even if f is called with the same arguments both times. A Unary Function may refer to local state, perform I/O, and so on. The expression f(x) is permitted to change f's state.

See also

Function Object overview, Generator, Binary Function Adaptable Unary Function

Binary Function

Category: functors

Component type: concept

Description

A Binary Function is a kind of function object: an object that is called as if it were an ordinary C++ function. A Binary Function is called with two arguments.

Refinement of

Assignable

Associated types

First argument typeThe type of the Binary Function's first argument.
Second argument typeThe type of the Binary Function's second argument.
Result typeThe type returned when the Binary Function is called

Notation

F A type that is a model of BinaryFunction

X The first argument type of F

Y The second argument type of F

Result The result type of F

f Object of type F

x Object of type X

y Object of type Y

Definitions

The domain of a Binary Function is the set of all ordered pairs (x, y) that are permissible values for its arguments.

The range of a Binary Function is the set of all possible value that it may return.

Valid expressions

NameExpressionReturn type
Function callf(x,y)Result

Expression semantics

NameExpressionPreconditionSemanticsPostcondition
Function callf(x,y)The ordered pair (x,y) is in f's domainCalls f with x and y as arguments, and returns a value of type Result [1]The return value is in f's range

Models

• Result (*)(X,Y)

Notes

[1] Two different invocations of f may return different results, even if f is called with the same arguments both times. A Binary Function may refer to local state, perform I/O, and so on. The expression f(x,y) is permitted to change f's state.

See also

Function Object overview, Generator, Unary Function, Adaptable Binary Function

Adaptable Generator

Category: functors

Component type: concept

Description

An Adaptable Generator is a Generator with a nested typedef that defines its result type. [1] This nested typedef makes it possible to use function object adaptors.

Refinement of

Generator

Associated types

Result typeF::result_typeThe type returned when the Generator is called

Notation

F A type that is a model of Adaptable Generator

Valid expressions

None, except for those defined by Generator

Models

The STL does not include any types that are models of Adaptable Generator. An example of a user-defined Adaptable Generator is as follows.

struct counter {

 typedef int result_type;

 counter() : n(0) {}

 result_type operator()() { return n++; }

 result_type n;

};

Notes

[1] Note the implication of this: a function pointer T (*f)() is a Generator, but not an Adaptable Generator: the expression f::result_type is nonsensical.

See also

Generator, Adaptable Unary Function, Adaptable Binary Function

Adaptable Unary Function

Category: functors

Component type: concept

Description

An Adaptable Unary Function is a Unary Function with nested typedefs that define its argument type and result type. [1] [2] These nested typedef make it possible to use function object adaptors.

Refinement of

Unary Function

Associated types

Argument typeF::argument_typeThe type of F's argument
Result typeF::result_typeThe type returned when the Unary Function is called

Notation

F A type that is a model of Unary Function

Valid expressions

None, except for those defined by Unary Function

Models

• negate

• identity

• pointer_to_unary_function

Notes

[1] Note the implication of this: a function pointer T (*f)(X) is a Unary Function, but not an Adaptable Unary Function: the expressions f::argument_type and f::result_type are nonsensical.

[2] When you define a class that is a model of Adaptable Unary Function, you must provide these typedefs. The easiest way to do this is to derive the class from the base class unary_function. This is an empty class, with no member functions or member variables; the only reason it exists is to make defining Adaptable Unary Functions more convenient. Unary_function is very similar to the base classes used by the iterator tag functions.

See also

Unary Function, Adaptable Generator, Adaptable Binary Function

Adaptable Binary Function

Category: functors

Component type: concept

Description

An Adaptable Binary Function is a Binary Function with nested typedefs that define its argument types and result type. [1] [2] These nested typedefs make it possible to use function object adaptors.

Refinement of

Binary Function

Associated types

First argument typeF::first_argument_typeThe type of F's first argument
Second argument typeF::second_argument_typeThe type of F's second argument
Result typeF::result_typeThe type returned when the Binary Function is called

Notation

F A type that is a model of Binary Function

Valid expressions

None, except for those defined by Binary Function

Models

• plus

• project1st

• pointer_to_binary_function

Notes

[1] Note the implication of this: a function pointer T (*f)(X,Y) is a Binary Function, but not an Adaptable Binary Function: the expressions f::first_argument_type, f::second_argument_type, and f::result_type are nonsensical.

[2] When you define a class that is a model of Adaptable Binary Function, you must provide these typedefs. The easiest way to do this is to derive the class from the base class binary_function. This is an empty class, with no member functions or member variables; the only reason it exists is to make defining Adaptable Binary Functions more convenient. Binary_function is very similar to the base classes used by the iterator tag functions.

See also

Binary Function, Adaptable Generator, Adaptable Unary Function

Predicates

Predicate

Category: functors

Component type: concept

Description

A Predicate is a Unary Function whose result represents the truth or falsehood of some condition. A Predicate might, for example, be a function that takes an argument of type int and returns true if the argument is positive.

Refinement of

Unary Function

Associated types

Result typeThe type returned when the Predicate is called. The result type must be convertible to bool.

Notation

F A type that is a model of Predicate

X The argument type of F

f Object of type F

x Object of type X

Valid expressions

NameExpressionReturn type
Function callf(x)Convertible to bool

Expression semantics

NameExpressionPreconditionSemanticsPostcondition
Function callf(x)x is in the domain of f.Returns true if the condition is satisfied, false if it is not.The result is either true or false.

Models

• bool (*)(int)

See also

Adaptable Predicate, Binary Predicate, Adaptable Binary Predicate

Binary Predicate

Category: functors

Component type: concept

Description

A Binary Predicate is a Binary Function whose result represents the truth or falsehood of some condition. A Binary Predicate might, for example, be a function that takes two arguments and tests whether they are equal.

Refinement of

Binary Function

Associated types

Result typeThe type returned when the Binary Predicate is called. The result type must be convertible to bool.

Notation

F A type that is a model of Binary Predicate

X The first argument type of F

Y The second argument type of F

f Object of type F

x Object of type X

y Object of type Y

Valid expressions

NameExpressionReturn type
Function callf(x,y)Convertible to bool

Expression semantics

NameExpressionPreconditionSemanticsPostcondition
Function callf(x,y)The ordered pair (x,y) is in the domain of f.Returns true if the condition is satisfied, false if it is not.The result is either true or false.

Models

• bool (*)(int,int)

• equal_to

See also

Predicate, Adaptable Predicate, Adaptable Binary Predicate

Adaptable Predicate

Category: functors

Component type: concept

Description

An Adaptable Predicate is a Predicate that is also an Adaptable Unary Function. That is, it is a Unary Function whose return type is bool, and that includes nested typedefs that define its argument type and return type.

Refinement of

Predicate, Adaptable Unary Function

Associated types

None, except for those associated with Predicate and Adaptable Unary Function.

Valid expressions

None, except for those defined by the Predicate and Adaptable Unary Function requirements.

Models

• logical_not

• unary_negate

See also

Predicate, Binary Predicate, Adaptable Binary Predicate

Adaptable Binary Predicate

Category: functors

Component type: concept

Description

An Adaptable Binary Predicate is a Binary Predicate that is also an Adaptable Binary Function. That is, it is a Binary Function whose return type is bool, and that includes nested typedef s that define its argument types and return type.

Refinement of

Predicate, Adaptable Binary Function

Associated types

None, except for those associated with Predicate and Adaptable Binary Function.

Valid expressions

None, except for those defined by the Predicate and Adaptable Binary Function requirements.

Models

• less

• equal_to

• logical_and

• logical_or

• binary_negate

See also

Binary Predicate, Predicate, Adaptable Predicate

Strict Weak Ordering

Category: functors

Component type: concept

Description

A Strict Weak Ordering is a Binary Predicate that compares two objects, returning true if the first precedes the second. This predicate must satisfy the standard mathematical definition of a strict weak ordering. The precise requirements are stated below, but what they roughly mean is that a Strict Weak Ordering has to behave the way that "less than" behaves: if a is less than b then b is not less than a, if a is less than b and b is less than c then a is less than c, and so on.

Refinement of

Binary Predicate

Associated types

First argument typeThe type of the Strict Weak Ordering's first argument.
Second argument typeThe type of the Strict Weak Ordering's second argument. The first argument type and second argument type must be the same.
Result typeThe type returned when the Strict Weak Ordering is called. The result type must be convertible to bool.

Notation

F A type that is a model of Strict Weak Ordering

X The type of Strict Weak Ordering's arguments.

f Object of type F

x, y, z Object of type X

Definitions

• Two objects x and y are equivalent if both f(x, y) and f(y, x) are false. Note that an object is always (by the irreflexivity invariant) equivalent to itself.

Valid expressions

None, except for those defined in the Binary Predicate requirements.

Expression semantics

NameExpressionPreconditionSemanticsPostcondition
Function callf(x, y)The ordered pair (x,y) is in the domain of fReturns true if x precedes y, and false otherwiseThe result is either true or false

Invariants

Irreflexivityf(x, x) must be false.
Antisymmetryf(x, y) implies !f(y, x)
Transitivityf(x, y) and f(y, z) imply f(x, z).
Transitivity of equivalenceEquivalence (as defined above) is transitive: if x is equivalent to y and y is equivalent to z, then x is equivalent to z. (This implies that equivalence does in fact satisfy the mathematical definition of an equivalence relation.) [1]

Models

• less<int>

• less<double>

• greater<int>

• greater<double>

Notes

[1] The first three axioms, irreflexivity, antisymmetry, and transitivity, are the definition of a partial ordering; transitivity of equivalence is required by the definition of a strict weak ordering. A total ordering is one that satisfies an even stronger condition: equivalence must be the same as equality.

See also

LessThan Comparable, less, Binary Predicate, function objects

MonoidOperation

Category: functors

Component type: concept

Description

A Monoid Operation is a special sort of Binary Function. A Binary Function must satisfy three conditions in order to be a Monoid Operation. First, its first argument type and second argument type must be the same, and its result type must be the same as its argument type. Second, there must be an identity element. Third, the operation must be associative. Examples of Monoid Operations are addition and multiplication. [1]

Refinement of

Binary Function

Associated types

Argument typeThe type of the Monoid Operation's first argument and second argument, and also the type returned when the Monoid Operation is returned.

Notation

F A type that is a model of MonoidOperation

T F's argument type.

f Object of type F

x, y, z Objects of type T

Definitions

A type F that is a model of binary function is associative if F's first argument type, second argument type, and result type are the same, and if, for every object f of type F and for every objects x, y, and z of F's argument type, f(x, f(y, z)) is the same as f(f(x, y), z). [2]

Valid Expressions

In addition to the expressions described in the Binary Function requirements, the following expressions must be valid.

NameExpressionReturn type
Function callf(x, y)T
Identity elementidentity_element(f)[3]T

Expression semantics

NameExpressionPreconditionSemantics
Function callf(x, y)x and y are in the domain of f.Calls f with x and y as arguments.
Identity elementidentity_element(f)Returns the monoid's identity element. That is, the return value is a value id of type T such that, for all x in the domain of f, f(x, id) and f(id, x) both return x.

Invariants

AssociativityFor any x, y, and z of type T, f(x, f(y, z)) and f(f(x, y), z) return the same value. [4]
Identity element.There exists some element id of type T such that, for all x of type T, f(x, id) and f(id, x) both return x. The expression identity_element(f) returns id.

Models

• plus<int>

• multiplies<double>

Notes

[1] A monoid is one of three closely related algebraic structures. A semigroup is a set S, and a binary operation *, with the properties that * is closed on S (that is, if x and y are elements of S then x * y is also a member of S) and that * is associative (that is, if x, y, and z are elements of S, then x * (y * z) = (x * y) * z). A monoid is a semigroup that has an identity element. That is, there exists some element id such that, for all x in S, x * id = id * x = x. Finally, a group is a monoid with the property that every element has an inverse. That is, for every x in S, there exists an element xi such that x * xi = xi * x = id. As an example, the set of real numbers under multiplication is a monoid (the identity element is 1), but it isn't a group. It isn't a group because 0 has no inverse.

[2] Mathematics textbooks typically write this as an equation, instead of using words like "is the same as". We can't use equality in this definition, however, because F's argument type might not be equality comparable. If F's argument type is equality comparable, however, then these two expression are expected to be equal: the condition of associativity becomes f(x, f(y, z)) == f(f(x, y), z)

[3] This is implemented as an overloaded function. The function identity_element is defined, in the standard header functional , and the nonstandard backward-compatibility header function.h, for arguments of type plus<T> and multiplies<T>. If you define a new Monoid Operation F (matrix multiplication, for example), you must overload identity_element for arguments of type F. The identity_element function is an SGI extension; it is not part of the C++ standard.

[4] Associativity is not the same as commutativity. That is, the requirement that x * (y * z) == (x * y) * z is completely unrelated to the requirement that x * y == y * x . Monoid operations are required to be associative, but they are not required to be commutative. As an example, square matrices under multiplication form a monoid even though matrix multiplication is not commutative.

See also

Binary Function, plus, multiplies

Random Number Generator

Category: functors

Component type: concept

Description

A Random Number Generator is a function object that can be used to generate a random sequence of integers. That is: if f is a Random Number Generator and N is a positive integer, then f(N) will return an integer less than N and greater than or equal to 0. If f is called many times with the same value of N, it will yield a sequence of numbers that is uniformly distributed [1] in the range [0, N). [2]

Refinement of

Unary Function

Associated types

Argument typeThe type of the Random Number Generator's argument. This must be an integral type.
Result typeThe type returned when the Random Number Generator is called. It must be the same as the argument type.

Notation

F A type that is a model of Random Number Generator.

Integer The argument type of F.

f Object of type F.

N Object of type Integer

Definitions

The domain of a Random Number Generator (i.e. the set of permissible values for its argument) is the set of numbers that are greater than zero and less than some maximum value.

The range of a Random Number Generator is the set of nonnegative integers that are less than the Random Number Generator's argument.

Valid expressions

None, except for those defined by Unary Function.

Expression semantics

NameExpressionPreconditionSemanticsPostcondition
Function callf(N)N is positive.Returns a pseudo-random number of type Integer. [2]The return value is less than N , and greater than or equal to 0.

Invariants

UniformityIn the limit as f is called many times with the same argument N, every integer in the range [0, N) will appear an equal number of times.

Notes

[1] Uniform distribution means that all of the numbers in the range [0, N) appear with equal frequency. Or, to put it differently, the probability for obtaining any particular value is 1/N.

[2] Random number generators are a very subtle subject: a good random number generator must satisfy many statistical properties beyond uniform distribution. See section 3.4 of Knuth for a discussion of what it means for a sequence to be random, and section 3.2 for several algorithms that may be used to write random number generators. (D. E. Knuth, The Art of Computer Programming. Volume 2: Seminumerical Algorithms, third edition. Addison-Wesley, 1998.)

Predefined function objects

Arithmetic operations

plus<T>

Category: functors

Component type: type

Description

Plus<T> is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class plus<T> and x and y are objects of class T, then f(x,y) returns x+y.

Example

Each element in V3 will be the sum of the corresponding elements in V1 and V2

const int N = 1000;

vector<double> V1(N);

vector<double> V2(N);

vector<double> V3(N);

iota(V1.begin(), V1.end(), 1);

fill(V2.begin(), V2.end(), 75);

assert(V2.size() >= V1.size() && V3.size() >= V1.size());

transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), plus<double>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe function object's argument type and result type.

Model of

Adaptable Binary Function, Default Constructible

Type requirements

T must be a numeric type; if x and y are objects of type T, then x+y must be defined and must have a return type that is convertible to T. T must be Assignable.

Public base classes

binary_function<T, T, T>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: T
second_argument_typeAdaptable Binary FunctionThe type of the second argument: T
result_typeAdaptable Binary FunctionThe type of the result: T
T operator()(const T& x, const T& y)Adaptable Binary FunctionFunction call operator. The return value is x + y.
plus()Default ConstructibleThe default constructor.

New members

All of plus's members are defined in the Adaptable Binary Function and Default Constructible requirements. Plus does not introduce any new members.

Notes

See also

The Function Object overview, Adaptable Binary Function, binary_function, minus, multiplies, divides, modulus, negate

minus<T>

Category: functors

Component type: type

Description

Minus<T> is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class minus<T> and x and y are objects of class T, then f(x,y) returns x-y.

Example

Each element in V3 will be the difference of the corresponding elements in V1 and V2

const int N = 1000;

vector<double> V1(N);

vector<double> V2(N);

vector<double> V3(N);

iota(V1.begin(), V1.end(), 1);

fill(V2.begin(), V2.end(), 75);

assert(V2.size() >= V1.size() && V3.size() >= V1.size());

transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), minus<double>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe function object's argument type and result type.

Model of

Adaptable Binary Function, Default Constructible

Type requirements

T must be a numeric type; if x and y are objects of type T, then x-y must be defined and must have a return type that is convertible to T. T must be Assignable.

Public base classes

binary_function<T, T, T>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: T
second_argument_typeAdaptable Binary FunctionThe type of the second argument: T
result_typeAdaptable Binary FunctionThe type of the result: T
T operator()(const T& x, const T& y)Adaptable Binary FunctionFunction call operator. The return value is x - y.
minus()Default ConstructibleThe default constructor.

New members

All of minus's members are defined in the Adaptable Binary Function and Default Constructible requirements. Minus does not introduce any new members.

See also

The Function Object overview, Adaptable Binary Function, binary_function, plus, multiplies, divides, modulus, negate

multiplies<T>

Category: functors

Component type: type

Description

Multiplies<T> [1] is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class multiplies<T> and x and y are objects of class , then f(x,y) returns x*y.

Example

Each element in V3 will be the product of the corresponding elements in V1 and V2

const int N = 1000;

vector<double> V1(N);

vector<double> V2(N);

vector<double> V3(N);

iota(V1.begin(), V1.end(), 1);

fill(V2.begin(), V2.end(), 75);

assert(V2.size() >= V1.size() && V3.size() >= V1.size());

transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), multiplies<double>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe function object's argument type and result type.

Model of

Adaptable Binary Function, Default Constructible

Type requirements

T must be a numeric type; if x and y are objects of type T, then x*y must be defined and must have a return type that is convertible to T. T must be Assignable.

Public base classes

binary_function<T, T, T>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: T
second_argument_typeAdaptable Binary FunctionThe type of the second argument: T
result_typeAdaptable Binary FunctionThe type of the result: T
T operator()(const T& x, const T& y)Adaptable Binary FunctionFunction call operator. The return value is x * y.
multiplies() [1]Default ConstructibleThe default constructor.

New members

All of multiplies's members are defined in the Adaptable Binary Function and Default Constructible requirements. Multiplies does not introduce any new members.

Notes

[1] Warning: the name of this function object has been changed from imes to multiplies. The name was changed for two reasons. First, it is called multiplies in the C++ standard. Second, the name times conflicts with a function in the Unix header <sys/times.h>.

See also

The Function Object overview, Adaptable Binary Function, binary_function, plus, minus, divides, modulus, negate

divides<T>

Category: functors

Component type: type

Description

Divides<T> is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class divides<T> and x and y are objects of class T, then f(x,y) returns x/y.

Example

Each element in V3 will be the quotient of the corresponding elements in V1 and V2

const int N = 1000;

vector<double> V1(N);

vector<double> V2(N);

vector<double> V3(N);

iota(V1.begin(), V1.end(), 1);

fill(V2.begin(), V2.end(), 75);

assert(V2.size() >= V1.size() && V3.size() >= V1.size());

transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), divides<double>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe function object's argument type and result type.

Model of

Adaptable Binary Function, Default Constructible

Type requirements

T must be a numeric type; if x and y are objects of type T, then x/y must be defined and must have a return type that is convertible to T. T must be Assignable.

Public base classes

binary_function<T, T, T>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: T
second_argument_typeAdaptable Binary FunctionThe type of the second argument: T
result_typeAdaptable Binary FunctionThe type of the result: T
T operator()(const T& x, const T& y)Adaptable Binary FunctionFunction call operator. The return value is x / y.
divides()Default ConstructibleThe default constructor.

New members

All of divides's members are defined in the Adaptable Binary Function and Default Constructible requirements. Divides does not introduce any new members.

See also

The Function Object overview, Adaptable Binary Function, binary_function, plus, minus, multiplies, modulus, negate

modulus<T>

Category: functors

Component type: type

Description

Modulus<T> is a function object. Specifically, it is an Adaptable Binary Function. If f is an object of class modulus<T> and x and y are objects of class , then f(x,y) returns x%y.

Example

Each element in V3 will be the modulus of the corresponding elements in V1 and V2

const int N = 1000;

vector<double> V1(N);

vector<double> V2(N);

vector <double> V3(N);

iota(V1.begin(), V1.end(), 1);

fill(V2.begin(), V2.end(), 75);

assert(V2.size() >= V1.size() && V3.size() >= V1.size());

transform(V1.begin(), V1.end(), V2.begin(), V3.begin(), modulus<int>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe function object's argument type and result type.

Model of

Adaptable Binary Function, Default Constructible

Type requirements

T must be a numeric type; if x and y are objects of type T, then x%y must be defined and must have a return type that is convertible to T. T must be Assignable.

Public base classes

binary_function<T, T, T>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: T
second_argument_typeAdaptable Binary FunctionThe type of the second argument: T
result_typeAdaptable Binary FunctionThe type of the result: T
T operator()(const T& x, const T& y)Adaptable Binary FunctionFunction call operator. The return value is x % y.
modulus()Default ConstructibleThe default constructor.

New members

All of modulus's members are defined in the Adaptable Binary Function and Default Constructible requirements. Modulus does not introduce any new members.

See also

The Function Object overview, Adaptable Binary Function, binary_function, plus, minus, multiplies, divides, negate

negate<T>

Category: functors

Component type: type

Description

Negate<T> is a function object. Specifically, it is an Adaptable Unary Function. If f is an object of class negate<T> and x is an object of class T, then f(x) returns -x.

Example

Each element in V2 will be the negative (additive inverse) of the corresponding element in V1.

const int N = 1000;

vector<double> V1(N);

vector<double> V2(N);

iota(V1.begin(), V1.end(), 1);

assert(V2.size() >= V1.size());

transform(V1.begin(), V1.end(), V2.begin(), negate<int>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe function object's argument type and result type.

Model of

Adaptable Unary Function, Default Constructible

Type requirements

T must be a numeric type; if x is an object of type T, then -x must be defined and must have a return type that is convertible to T. T must be Assignable.

Public base classes

unary_function<T, T>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the second argument: T
result_typeAdaptable Unary FunctionThe type of the result: T
T operator()(const T& x)Adaptable Unary FunctionFunction call operator. The return value is -x.
negate()Default ConstructibleThe default constructor.

New members

All of negate's members are defined in the Adaptable Unary Function and Default Constructible requirements. Negate does not introduce any new members.

See also

The Function Object overview, Adaptable Unary Function, unary_function, plus, minus, multiplies, divides, modulus

Comparisons

equal_to<T>

Category: functors

Component type: type

Description

Equal_to<T> is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class equal_to<T> and x and y are objects of class T, then f(x,y) returns true if x == y and false otherwise.

Example

Rearrange a vector such that all of the elements that are equal to zero precede all nonzero elements.

vector<int> V;

partition(V.begin(), V.end(), bind2nd(equal_to<int>(), 0));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of equal_to's arguments.

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T is EqualityComparable.

Public base classes

binary_function<T, T, bool>.

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary PredicateThe type of the first argument: T
second_argument_typeAdaptable Binary PredicateThe type of the second argument: T
result_typeAdaptable Binary PredicateThe type of the result: bool
equal_to()DefaultConstructibleThe default constructor.
bool operator()(const T& x, const T& y)Binary FunctionFunction call operator. The return value is x == y.

New members

All of equal_to's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Equal_to does not introduce any new members.

See also

The function object overview, Adaptable Binary Predicate, not_equal_to, greater, less, greater_equal, less_equal

not_equal_to<T>

Category: functors

Component type: type

Description

Not_equal_to<T> is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class not_equal_to<T> and x and y are objects of class T, then f(x,y) returns true if x != y and false otherwise.

Example

Finds the first nonzero element in a list.

list<int> L;

list<int>::iterator first_nonzero = find_if(L.begin(), L.end(), bind2nd(not_equal_to<int>(), 0));

assert(first_nonzero == L.end() || *first_nonzero != 0);

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of not_equal_to's arguments.

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T is EqualityComparable.

Public base classes

binary_function<T, T, bool>.

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary PredicateThe type of the first argument: T
second_argument_typeAdaptable Binary PredicateThe type of the second argument: T
result_typeAdaptable Binary PredicateThe type of the result: bool
not_equal_to()DefaultConstructibleThe default constructor.
bool operator()(const T& x, const T& y)Binary FunctionFunction call operator. The return value is x != y.

New members

All of not_equal_to's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Not_equal_to does not introduce any new members.

See also

The function object overview, Adaptable Binary Predicate, equal_to, greater, less, greater_equal, less_equal

less<T>

Category: functors

Component type: type

Description

Less<T> is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class less<T> and x and y are objects of class T, then f(x,y) returns true if x < y and false otherwise.

Example

Finds the first negative element in a list.

list<int> L;

list<int>::iterator first_negative = find_if(L.begin(), L.end(), bind2nd(less<int>(), 0));

assert(first_negative == L.end() || *first_negative < 0);

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of less's arguments.

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T is LessThan Comparable.

Public base classes

binary_function<T, T, bool>.

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary PredicateThe type of the first argument: T
second_argument_typeAdaptable Binary PredicateThe type of the second argument: T
result_typeAdaptable Binary PredicateThe type of the result: bool
less()DefaultConstructibleThe default constructor.
bool operator()(const T& x, const T& y)Binary FunctionFunction call operator. The return value is x < y.

New members

All of less's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. less does not introduce any new members.

See also

The function object overview, Strict Weak Ordering, Adaptable Binary Predicate, LessThan Comparable, equal_to, not_equal_to, greater, greater_equal, less_equal

greater<T>

Category: functors

Component type: type

Description

Greater<T> is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class greater<T> and x and y are objects of class T, then f(x,y) returns true if x > y and false otherwise.

Example

Sort a vector in descending order, rather than the default ascending order.

vector<int> V;

sort(V.begin(), V.end(), greater<int>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of greater's arguments.

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T is LessThan Comparable.

Public base classes

binary_function <T, T, bool>.

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary PredicateThe type of the first argument: T
second_argument_typeAdaptable Binary PredicateThe type of the second argument: T
result_typeAdaptable Binary PredicateThe type of the result: bool
greater()DefaultConstructibleThe default constructor.
bool operator()(const T& x, const T& y)Binary FunctionFunction call operator. The return value is x > y.

New members

All of greater's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Greater does not introduce any new members.

See also

The function object overview, Adaptable Binary Predicate, LessThan Comparable, equal_to, not_equal_to, less, greater_equal, less_equal

less_equal<T>

Category: functors

Component type: type

Description

Less_equal<T> is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class less_equal<T> and x and y are objects of class T, then f(x,y) returns true if x <= y and false otherwise.

Example

Finds the first non-positive element in a list.

list<int> L;

list<int>::iterator first_nonpositive = find_if(L.begin(), L.end(), bind2nd(less_equal<int>(), 0));

assert(first_nonpositive == L.end() || *first_nonpositive <= 0);

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of less_equal's arguments.

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T is LessThan Comparable.

Public base classes

binary_function<T, T, bool>.

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary PredicateThe type of the first argument: T
second_argument_typeAdaptable Binary PredicateThe type of the second argument: T
result_typeAdaptable Binary PredicateThe type of the result: bool
less_equal()DefaultConstructibleThe default constructor.
bool operator()(const T& x, const T& y)Binary FunctionFunction call operator. The return value is x <= y.

New members

All of less_equal's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Less_equal does not introduce any new members.

See also

The function object overview, Adaptable Binary Predicate, equal_to, not_equal_to, greater, less, greater_equal

greater_equal<T>

Category: functors

Component type: type

Description

Greater_equal<T> is a function object. Specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class greater_equal<T> and x and y are objects of class T, then f(x,y) returns true if x >= y and false otherwise.

Example

Find the first nonnegative element in a list.

list<int> L;

list<int>::iterator first_nonnegative = find_if(L.begin(), L.end(), bind2nd(greater_equal<int>(), 0));

assert(first_nonnegative == L.end() || *first_nonnegative >= 0);

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of greater_equal's arguments.

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T is LessThan Comparable.

Public base classes

binary_function<T, T, bool>.

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary PredicateThe type of the first argument: T
second_argument_typeAdaptable Binary PredicateThe type of the second argument: T
result_typeAdaptable Binary PredicateThe type of the result: bool
greater_equal()DefaultConstructibleThe default constructor.
bool operator()(const T& x, const T& y)Binary FunctionFunction call operator. The return value is x >= y.

New members

All of greater_equal's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Greater_equal does not introduce any new members.

See also

The function object overview, Adaptable Binary Predicate, equal_to, not_equal_to, greater less, less_equal

Logical operations

logical_and<T>

Category: functors

Component type: type

Description

Logical_and<T> is a function object; specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class logical_and<T> and x and y are objects of class T (where T is convertible to bool) then f(x,y) returns true if and only if both x and y are true. [1]

Example

Finds the first element in a list that lies in the range from 1 to 10.

list<int> L;

list<int>::iterator in_range = find_if(L.begin(), L.end(), compose2(logical_and<bool>(), bind2nd(greater_equal<int>(), 1), bind2nd(less_equal<int>(), 10)));

assert(in_range == L.end() || (*in_range >= 1 && *in_range <= 10));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of logical_and's arguments

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T must be convertible to bool.

Public base classes

binary_function<T, T, bool>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: T
second_argument_typeAdaptable Binary FunctionThe type of the second argument: T
result_typeAdaptable Binary FunctionThe type of the result: bool
bool operator()(const T& x, const T& y) constBinary FunctionFunction call operator. The return value is x && y.
logical_and()Default ConstructibleThe default constructor.

New members

All of logical_and's members are defined in the Adaptable Binary Function and Default Constructible requirements. Logical_and does not introduce any new members.

Notes

[1] Logical_and and logical_or are not very useful by themselves. They are mainly useful because, when combined with the function object adaptor binary_compose, they perform logical operations on other function objects.

See also

The function object overview, logical_or, logical_not.

logical_or<T>

Category: functors

Component type: type

Description

Logical_or<T> is a function object; specifically, it is an Adaptable Binary Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class logical_and<T> and x and y are objects of class T (where T is convertible to bool) then f(x,y) returns true if and only if either x or y is true. [1]

Example

Finds the first instance of either ' ' or '\n' in a string.

char str[MAXLEN];

const char* wptr = find_if(str, str + MAXLEN, compose2(logical_or<bool>(), bind2nd(equal_to<char>(), ' '), bind2nd(equal_to<char>(), '\n')));

assert(wptr == str + MAXLEN || *wptr == ' ' || *wptr == '\n');

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of logical_or's arguments

Model of

Adaptable Binary Predicate, DefaultConstructible

Type requirements

T must be convertible to bool.

Public base classes

binary_function<T, T, bool>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: T
second_argument_typeAdaptable Binary FunctionThe type of the second argument: T
result_typeAdaptable Binary FunctionThe type of the result: bool
bool operator()(const T& x, const T& y) constBinary Function Functioncall operator. The return value is x || y.
logical_or()Default ConstructibleThe default constructor.

New members

All of logical_or's members are defined in the Adaptable Binary Function and Default Constructible requirements. Logical_or does not introduce any new members.

Notes

[1] Logical_and and logical_or are not very useful by themselves. They are mainly useful because, when combined with the function object adaptor binary_compose, they perform logical operations on other function objects.

See also

The function object overview, logical_and, logical_not.

logical_not<T>

Category: functors

Component type: type

Description

Logical_not<T> is a function object; specifically, it is an Adaptable Predicate, which means it is a function object that tests the truth or falsehood of some condition. If f is an object of class logical_not<T> and x is an object of class T (where T is convertible to bool) then f(x) returns true if and only if x is false.

Example

Transforms a vector of bool into its logical complement.

vector<bool> V;

transform(V.begin(), V.end(), V.begin(), logical_not<bool>());

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
TThe type of logical_not's argument

Model of

Adaptable Predicate, DefaultConstructible

Type requirements

T must be convertible to bool.

Public base classes

unary_function<T, bool>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the second argument: T
result_typeAdaptable Unary FunctionThe type of the result: bool
bool operator()(const T& x) constUnary Function Function call operator.The return value is !x.
logical_not()Default ConstructibleThe default constructor.

See also

The function object overview, logical_or, logical_and.

Generalized identity operations

identity<T>

Category: functors

Component type: type

Description

Identity is a Unary Function that represents the identity function: it takes a single argument x, and returns x.

Example

int main() {

 int x = 137;

 identity<int> id;

 assert(x == id(x));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. This class is an SGI extension; it is not part of the C++ standard.

Template parameters

ParameterDescription
TThe function object's argument type, and return type. [1]

Model of

Adaptable Unary Function

Type requirements

None.

Public base classes

unary_function<T, T>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of identity's argument: T.
result_typeAdaptable Unary FunctionThe type of the result: T. [1]
const T& operator()(const T&) constAdaptable Unary FunctionFunction call. The return value is simply the argument.

New members

All of identity's members are defined in the Adaptable Unary Function requirements. Identity does not introduce any new members.

Notes

[1] It is essential that the return type and the argument type are the same: generalizing identity to allow them to differ would not work. The reason is that identity returns a const reference to its argument, rather than a copy of its argument. If identity were allowed to perform a conversion, then this would be a dangling reference.

See also

The function object overview, select1st, select2nd, project1st, project2nd

project1st<Arg1, Arg2>

Category: functors

Component type: type

Description

Project1st is a function object that takes two arguments and returns its first argument; the second argument is unused. It is essentially a generalization of identity to the case of a Binary Function.

Example

int main() {

 vector<int> v1(10, 137);

 vector<char*> v2(10, (char*) 0);

 vector<int> result(10);

 transform(v1.begin(), v1.end(), v2.begin(), result.begin(), project1st<int, char*>());

 assert(equal(v1.begin(), v1.end(), result.begin()));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. This function object is an SGI extension; it is not part of the C++ standard.

Template parameters

ParameterDescription
Arg1project1st's first argument type, and its result type.
Arg2project1st 's second argument type.

Model of

Adaptable Binary Function

Type requirements

None.

Public base classes

binary_function<Arg1, Arg2, Arg1>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of project1st's first argument: Arg1
second_argument_typeAdaptable Binary Function The type of project1st's second argument: Arg2
result_typeAdaptable Binary FunctionThe type of the result: Arg1.
Arg1 operator()(const Arg1& x, const Arg2&) constAdaptable Binary FunctionFunction call. The return value is x.

New members

All of project1st's members are defined in the Adaptable Binary Function requirements. project1st does not introduce any new members.

See also

Function objects, identity, project2nd, select1st, select2nd

project2nd<Arg1, Arg2>

Category: functors

Component type: type

Description

Project2nd is a function object that takes two arguments and returns its second argument; the first argument is unused. It is essentially a generalization of identity to the case of a Binary Function.

Example

int main() {

 vector<char*> v1(10, (char*) 0);

 vector<int> v2(10, 137);

 vector<int> result(10);

 transform(v1.begin(), v1.end(), v2.begin(), result.begin(), project2nd<char*, int>());

 assert(equal(v2.begin(), v2.end(), result.begin()));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. This function object is an SGI extension; it is not part of the C++ standard.

Template parameters

ParameterDescription
Arg1project2nd's first argument type.
Arg2project2nd's second argument type, and its result type.

Model of

Adaptable Binary Function

Type requirements

None.

Public base classes

binary_function<Arg1, Arg2, Arg2>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of project2nd's first argument: Arg1
second_argument_typeAdaptable Binary FunctionThe type of project2nd's second argument: Arg2
result_typeAdaptable Binary FunctionThe type of the result: Arg2.
Arg1 operator()(const Arg1&, const Arg2& y) constAdaptable Binary FunctionFunction call. The return value is y.

New members

All of project2nd's members are defined in the Adaptable Binary Function requirements. project2nd does not introduce any new members.

See also

Function objects, identity, project1st, select1st, select2nd

select1st<Pair>

Category: functors

Component type: type

Description

Select1st is a function object that takes a single argument, a pair [1], and returns the pair's first element.

Example

Print all of a map's keys.

int main() {

 map<int, double> M;

 M[1] = 0.3;

 M[47] = 0.8;

 M[33] = 0.1;

 transform(M.begin(), M.end(), ostream_iterator<int>(cout, " "), select1st<map<int, double>::value_type>());

 // The output is 1 33 47.

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. This function object is an SGI extension; it is not part of the C++ standard.

Template parameters

ParameterDescription
PairThe function object's argument type.

Model of

Adaptable Unary Function

Type requirements

There exist some types U and V such that Pair provides the same interface as a pair<U,V>. [1]

Public base classes

unary_function<Pair, Pair::first_type>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of select1st 's argument: Pair
result_typeAdaptable Unary Function The type of the result:Pair::first_type
const Pair::first_type& operator()(const Pair& p) constAdaptable Unary FunctionFunction call. The return value is p.first.

New members

All of select1st's members are defined in the Adaptable Unary Function requirements. Select1st does not introduce any new members.

Notes

[1] Pair is not actually required to be a pair<U,V> , but merely to support the same interface as pair. In almost all cases the template parameter will be a pair, but it is occasionally useful for it to be something else. One example is a struct that has the members first, second, and third.

See also

identity, select2nd, project1st, project2nd

select2nd<Pair>

Category: functors

Component type: type

Description

Select2nd is a function object that takes a single argument, a pair [1], and returns the pair's second element.

Example

Print all of a map's values.

int main() {

 map<int, double> M;

 M[1] = 0.3;

 M[47] = 0.8;

 M[33] = 0.1;

 transform(M.begin(), M.end(), ostream_iterator<double>(cout, " "), select2nd<map<int, double>::value_type>());

 // The output is 0.3 0.1 0.8

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. This function object is an SGI extension; it is not part of the C++ standard.

Template parameters

ParameterDescription
PairThe function object's argument type.

Model of

Adaptable Unary Function

Type requirements

There exist some types U and V such that Pair provides the same interface as a pair<U,V>. [1]

Public base classes

unary_function<Pair, Pair::second_type>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of select2nd's argument: Pair
result_typeAdaptable Unary FunctionThe type of the result: Pair::second_type
const Pair::second_type& operator()(const Pair& p) constAdaptable Unary FunctionFunction call. The return value is p.second.

New members

All of select2nd's members are defined in the Adaptable Unary Function requirements. Select2nd does not introduce any new members.

Notes

[1] Pair is not actually required to be a pair<U,V>, but merely to support the same interface as pair. In almost all cases the template parameter will be a pair, but it is occasionally useful for it to be something else. One example is a struct that has the members first, second, and third.

See also

identity, select1st, project1st, project2nd

subtractive_rng

Category: functors

Component type: type

Description

Subtractive_rng is a Random Number Generator based on the subtractive method [1]. It is a Unary Function: it takes a single argument N, an unsigned int, and returns an unsigned int that is less than N. Successive calls to the same subtractive_rng object [2] yield a pseudo-random sequence.

Example

int main() {

 subtractive_rng R;

 for (int i = 0; i < 20; ++i) cout << R(5) << ' ';

 cout << endl;

}

// The output is 3 2 3 2 4 3 1 1 2 2 0 3 4 4 4 4 2 1 0 0

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. This function object is an SGI extension; it is not part of the C++ standard.

Template parameters

None.

Model of

Random Number Generator, Adaptable Unary Function

Type requirements

None.

Public base classes

unary_function<unsigned int, unsigned int>

Members

ParameterDescriptionDefault
argument_typeAdaptable Unary FunctionThe type of a subtractive_rng's argument: unsigned int.
result_typeAdaptable Unary FunctionThe type of the result: unsigned int.
subtractive_rng(unsigned int seed)subtractive_rngSee below.
subtractive_rng()subtractive_rngSee below.
unsigned int operator()(unsigned int N)Adaptable Unary FunctionFunction call. Returns a pseudo-random number in the range [0, N).
void initialize(unsigned int seed)subtractive_rngSee below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to subtractive_rng.

MemberDescription
subtractive_rng(unsigned int seed)The constructor. Creates a subtractive_rng whose internal state is initialized using seed.
subtractive_rng()The default constructor. Creates a subtractive_rng initialized using a default value.
void initialize(unsigned int seed)Re-initializes the internal state of the subtractive_rng , using the value seed.

Notes

[1] See section 3.6 of Knuth for an implementation of the subtractive method in FORTRAN. Section 3.2.2 of Knuth analyzes this class of algorithms. (D. E. Knuth, The Art of Computer Programming. Volume 2: Seminumerical Algorithms, second edition. Addison-Wesley, 1981.)

[2] Note that the sequence produced by a subtractive_rng is completely deterministic, and that the sequences produced by two different subtractive_rng objects are independent of each other. That is: if R1 is a subtractive_rng, then the values returned when R1 is called depend only on R1's seed and on the number of times that R1 has been called. Calls to other subtractive_rng objects are irrelevant. In implementation terms, this is because the class subtractive_rng contains no static members.

See also

Random Number Generator

Function object adaptors

binder1st<AdaptableBinaryFunction>

Categories: functors, adaptors

Component type: type

Description

Binder1st is a function object adaptor: it is used to transform an adaptable binary function into an adaptable unary function. Specifically, if f is an object of class binder1st<AdaptableBinaryFunction>, then f(x) returns F(c, x), where F is an object of class AdaptableBinaryFunction and where c is a constant. Both F and c are passed as arguments to binder1st 's constructor. [1]

The easiest way to create a binder1st is not to call the constructor explicitly, but instead to use the helper function bind1st.

Example

Finds the first nonzero element in a list.

list<int> L;

list<int>::iterator first_nonzero = find_if(L.begin(), L.end(), bind1st(not_equal_to<int>(), 0));

assert(first_nonzero == L.end() || *first_nonzero != 0);

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
AdaptableBinaryFunctionThe type of the binary function whose first argument is being bound to a constant.

Model of

Adaptable Unary Function

Type requirements

AdaptableBinaryFunction must be a model of Adaptable Binary Function.

Public base classes

unary_function<AdaptableBinaryFunction::second_argument_type, AdaptableBinaryFunction::result_type>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the function object's argument, which is AdaptableBinaryFunction::second_argument_type
result_typeAdaptable Unary FunctionThe type of the result: AdaptableBinaryFunction::result_type
result_type operator()(const argument_type& x) constAdaptable Unary FunctionFunction call. Returns F(c, x), where F and c are the arguments with which this binder1st was constructed.
binder1st(const AdaptableBinaryFunction& F, AdaptableBinaryFunction::first_argument_type c)binder1stSee below
template <class AdaptableBinaryFunction, class T> binder1st<AdaptableBinaryFunction> bind1st(const AdaptableBinaryFunction& F, const T& c);binder1stSee below

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to binder1st.

MemberDescription
binder1st(const AdaptableBinaryFunction& F, AdaptableBinaryFunction::first_argument_type c)The constructor. Creates a binder1st such that calling it with the argument x (where x is of type AdaptableBinaryFunction::second_argument_type ) corresponds to the call F(c, x).
template <class AdaptableBinaryFunction, class T> binder1st<AdaptableBinaryFunction> bind1st(const AdaptableBinaryFunction& F, const T& c);If F is an object of type AdaptableBinaryFunction, then bind1st(F, c) is equivalent to binder1st<AdaptableBinaryFunction>(F, c), but is more convenient. The type T must be convertible to AdaptableBinaryFunction::first_argument_type. This is a global function, not a member function.

Notes

[1] Intuitively, you can think of this operation as "binding" the first argument of a binary function to a constant, thus yielding a unary function. This is a special case of a closure.

See also

The function object overview, binder2nd, Adaptable Unary Function, Adaptable Binary Function

binder2nd<AdaptableBinaryFunction>

Categories: functors, adaptors

Component type: type

Description

Binder2nd is a function object adaptor: it is used to transform an adaptable binary function into an adaptable unary function. Specifically, if f is an object of class binder2nd<AdaptableBinaryFunction>, then f(x) returns F(x, c), where F is an object of class AdaptableBinaryFunction and where c is a constant. Both F and c are passed as arguments to binder2nd's constructor. [1]

The easiest way to create a binder2nd is not to call the constructor explicitly, but instead to use the helper function bind2nd.

Example

Finds the first positive number in a list.

list<int> L;

list<int>::iterator first_positive = find_if(L.begin(), L.end(), bind2nd(greater<int>(), 0));

assert(first_positive == L.end() || *first_positive > 0);

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
AdaptableBinaryFunctionThe type of the binary function whose second argument is being bound to a constant.

Model of

Adaptable Unary Function

Type requirements

AdaptableBinaryFunction must be a model of Adaptable Binary Function.

Public base classes

unary_function<AdaptableBinaryFunction::first_argument_type, AdaptableBinaryFunction::result_type>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the function object's argument, which is AdaptableBinaryFunction::first_argument_type
result_typeAdaptable Unary FunctionThe type of the result: AdaptableBinaryFunction::result_type
result_type operator()(const argument_type& x) constAdaptable Unary FunctionFunction call. Returns F(x, c) , where F and c are the arguments with which this binder1st was constructed.
binder2nd(const AdaptableBinaryFunction& F, AdaptableBinaryFunction::second_argument_type c)binder2ndSee below
template <class AdaptableBinaryFunction, class T> binder2nd<AdaptableBinaryFunction> bind2nd(const AdaptableBinaryFunction& F, const T& c);binder2ndSee below

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to binder2nd.

MemberDescription
binder2nd(const AdaptableBinaryFunction& F, AdaptableBinaryFunction::second_argument_type c)The constructor. Creates a binder2nd such that calling it with the argument x (where x is of type AdaptableBinaryFunction::first_argument_type) corresponds to the call F(x, c).
template <class AdaptableBinaryFunction, class T> binder2nd<AdaptableBinaryFunction> bind2nd(const AdaptableBinaryFunction& F, const T& c);If F is an object of type AdaptableBinaryFunction, then bind2nd(F, c) is equivalent to binder2nd<AdaptableBinaryFunction>(F, c), but is more convenient. The type T must be convertible to AdaptableBinaryFunction::second_argument_type. This is a global function, not a member function.

Notes

[1] Intuitively, you can think of this operation as "binding" the second argument of a binary function to a constant, thus yielding a unary function. This is a special case of a closure.

See also

The function object overview, binder1st, Adaptable Unary Function, Adaptable Binary Function

ptr_fun

Categories: functors, adaptors

Component type: function

Prototype

template <class Arg, class Result>

pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg));

template <class Arg1, class Arg2, class Result>

pointer_to_binary_function<Arg1, Arg2, Result> ptr_fun(Result (*x)(Arg1, Arg2));

Description

Ptr_fun takes a function pointer as its argument and returns a function pointer adaptor, a type of function object. It is actually two different functions, not one (that is, the name ptr_fun is overloaded). If its argument is of type Result (*)(Arg) then ptr_fun creates a pointer_to_unary_function, and if its argument is of type Result (*)(Arg1, Arg2) then ptr_fun creates a pointer_to_binary_function.

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Requirements on types

The argument must be a pointer to a function that takes either one or two arguments. The argument type(s) and the return type of the function are arbitrary, with the restriction that the function must return a value; it may not be a void function.

Example

See the examples in the discussions of pointer_to_unary_function and pointer_to_binary_function.

See also

Function Objects, pointer_to_unary_function, pointer_to_binary_function, Adaptable Unary Function, Adaptable Binary Function

pointer_to_unary_function<Arg, Result>

Categories: functors, adaptors

Component type: type

Description

Pointer_to_unary_function is a function object adaptor that allows a function pointer Result (*f)(Arg) to be treated as an Adaptable Unary Function. That is: if F is a pointer_to_unary_function<Arg, Result> that was initialized with an underlying function pointer f of type Result (*)(Arg), then F(x) calls the function f(x). The difference between f and F is that pointer_to_unary_function is an Adaptable Unary Function, i.e. it defines the nested typedef s argument_type and result_type.

Note that a function pointer of type Result (*)(Arg) is a perfectly good Unary Function object, and may be passed to an STL algorithm that expects an argument that is a Unary Function. The only reason for using the pointer_to_unary_function object is if you need to use an ordinary function in a context that requires an Adaptable Unary Function, e.g. as the argument of a function object adaptor.

Most of the time, you need not declare an object of type pointer_to_unary_function directly. It is almost always easier to construct one using the ptr_fun function.

Example

The following code fragment replaces all of the numbers in a range with their absolute values, using the standard library function fabs. There is no need to use a pointer_to_unary_function adaptor in this case.

transform(first, last, first, fabs);

The following code fragment replaces all of the numbers in a range with the negative of their absolute values. In this case we are composing fabs and negate. This requires that fabs be treated as an adaptable unary function, so we do need to use a pointer_to_unary_function adaptor.

transform(first, last, first, compose1(negate<double>, ptr_fun(fabs)));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
ArgThe function object's argument type
ResultThe function object's result type

Model of

Adaptable Unary Function

Type requirements

• Arg is Assignable.

• Result is Assignable.

Public base classes

unary_function<Arg, Result>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the function object's argument: Arg.
result_typeAdaptable Unary FunctionThe type of the result: Result
result_type operator()(argument_type x)Unary FunctionFunction call operator.
pointer_to_unary_function(Result (*f)(Arg))pointer_to_unary_functionSee below.
pointer_to_unary_function()pointer_to_unary_functionSee below.
template <class Arg, class Result> pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg));pointer_to_unary_functionSee below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to pointer_to_unary_function.

MemberDescription
pointer_to_unary_function(Result (*f)(Arg))The constructor. Creates a pointer_to_unary_function whose underlying function is f.
pointer_to_unary_function()The default constructor. This creates a pointer_to_unary_function that does not have an underlying C function, and that therefore cannot actually be called.
template <class Arg, class Result> pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg));If f is of type Result (*)(Arg) then ptr_fun(f) is equivalent to pointer_to_unary_function<Arg,Result>(f), but more convenient. This is a global function, not a member.

See also

pointer_to_binary_function, ptr_fun, Adaptable Unary Function

pointer_to_binary_function<Arg1, Arg2, Result>

Categories: functors, adaptors

Component type: type

Description

Pointer_to_binary_function is a function object adaptor that allows a function pointer Result (*f)(Arg1, Arg2) to be treated as an Adaptable Binary Function . That is: if F is a pointer_to_binary_function<Arg1, Arg2, Result> that was initialized with an underlying function pointer f of type Result (*)(Arg1, Arg2), then F(x, y) calls the function f(x, y). The difference between f and F is that pointer_to_binary_function is an Adaptable Binary Function, i.e. it defines the nested typedef s first_argument_type, second_argument_type, and result_type.

Note that a function pointer of type Result (*)(Arg1, Arg2) is a perfectly good Binary Function object, and may be passed to an STL algorithm that expects an argument that is a Binary Function . The only reason for using the pointer_to_binary_function class is if you need to use an ordinary function in a context that requires an Adaptable Binary Function, e.g. as the argument of a function object adaptor.

Most of the time, you need not declare an object of type pointer_to_binary_function directly. It is almost always easier to construct one using the ptr_fun function.

Example

The following code fragment finds the first string in a list that is equal to "OK". It uses the standard library function strcmp as an argument to a function object adaptor, so it must first use a pointer_to_binary_function adaptor to give strcmp the Adaptable Binary Function interface.

list<char*> L;

list<char*>::iterator item = find_if(L.begin(), L.end(), not1(binder2nd(ptr_fun(strcmp), "OK")));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
Arg1The function object's first argument type
Arg2The function object's second argument type
ResultThe function object's result type

Model of

Adaptable Binary Function

Type requirements

Arg1 is Assignable.

Arg2 is Assignable.

Result is Assignable.

Public base classes

binary_function<Arg1, Arg2, Result>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: Arg1.
second_argument_typeAdaptable Binary FunctionThe type of the second argument: Arg2
result_typeAdaptable Binary FunctionThe type of the result: Result
Result operator()(Arg1 x, Arg2 y)Binary FunctionFunction call operator.
pointer_to_binary_function(Result (*f)(Arg1, Arg2))pointer_to_binary_functionSee below.
pointer_to_binary_function()pointer_to_binary_functionSee below.
template <class Arg1, class Arg2, class Result> pointer_to_unary_function<Arg1, Arg2, Result> ptr_fun(Result (*x)(Arg1, Arg2));pointer_to_binary_functionSee below.

New members

These members are not defined in the Adaptable Binary Function requirements, but are specific to pointer_to_binary_function.

MemberDescription
pointer_to_binary_function(Result (*f)(Arg1, Arg2))The constructor. Creates a pointer_to_binary_function whose underlying function is f.
pointer_to_binary_function()The default constructor. This creates a pointer_to_binary_function that does not have an underlying function, and that therefore cannot actually be called.
template <class Arg1, class Arg2, class Result> pointer_to_unary_function<Arg1, Arg2, Result> ptr_fun(Result (*x)(Arg1, Arg2));If f is of type Result (*)(Arg1, Arg2) then ptr_fun(f) is equivalent to pointer_to_binary_function<Arg1,Arg2,Result>(f), but more convenient. This is a global function, not a member function.

See also

pointer_to_unary_function, ptr_fun, Adaptable Binary Function

unary_negate<AdaptablePredicate>

Categories: functors, adaptors

Component type: type

Description

Unary_negate is a function object adaptor: it is an Adaptable Predicate that represents the logical negation of some other Adaptable Predicate. That is: if f is an object of class unary_negate<AdaptablePredicate>, then there exists an object pred of class AdaptablePredicate such that f(x) always returns the same value as !pred(x). [1] There is rarely any reason to construct a unary_negate directly; it is almost always easier to use the helper function not1.

Example

Finds the first element in a list that does not lie in the range from 1 to 10.

list<int> L;

list<int>::iterator in_range = find_if(L.begin(), L.end(), not1(compose2(logical_and<bool>(), bind2nd(greater_equal<int>(), 1), bind2nd(less_equal<int>(), 10))));

assert(in_range == L.end() || !(*in_range >= 1 && *in_range <= 10));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
AdaptablePredicateThe type of the function object that this unary_negate is the logical negation of.

Model of

Adaptable Predicate

Type requirements

AdaptablePredicate must be a model of Adaptable Predicate.

Public base classes

unary_function<AdaptablePredicate::argument_type, bool>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the argument: AdaptablePredicate::argument_type
result_typeAdaptable Unary FunctionThe type of the result: bool
bool operator()(argument_type)Unary FunctionFunction call operator.
unary_negate(const AdaptablePredicate& pred)unary_negateSee below.
template <class AdaptablePredicate> unary_negate<AdaptablePredicate> not1(const AdaptablePredicate& pred);unary_negateSee below.

New members

These members are not defined in the Adaptable Predicate requirements, but are specific to unary_negate.

MemberDescription
unary_negate(const AdaptablePredicate& pred)The constructor. Creates a unary_negate<AdaptablePredicate> whose underlying predicate is pred.
template <class AdaptablePredicate> unary_negate<AdaptablePredicate> not1(const AdaptablePredicate& pred);If p is of type AdaptablePredicate then not1(p) is equivalent to unary_negate<AdaptablePredicate>(p), but more convenient. This is a global function, not a member function.

Notes

[1] Strictly speaking, unary_negate is redundant. It can be constructed using the function object logical_not and the adaptor unary_compose.

See also

The function object overview, Adaptable Predicate, Predicate, binary_negate, unary_compose, binary_compose

binary_negate<AdaptableBinaryPredicate>

Categories: functors, adaptors

Component type: type

Description

Binary_negate is a function object adaptor: it is an Adaptable Binary Predicate that represents the logical negation of some other Adaptable Binary Predicate . That is: if f is an object of class binary_negate<AdaptableBinaryPredicate>, then there exists an object pred of class AdaptableBinaryPredicate such that f(x,y) always returns the same value as !pred(x,y). There is rarely any reason to construct a binary_negate directly; it is almost always easier to use the helper function not2.

Example

Finds the first character in a string that is neither ' ' nor '\n'.

char str[MAXLEN];

const char* wptr = find_if(str, str + MAXLEN, compose2(not2(logical_or<bool>()), bind2nd(equal_to<char>(), ' '), bind2nd(equal_to<char>(), '\n')));

assert(wptr == str + MAXLEN || !(*wptr == ' ' || *wptr == '\n'));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
AdaptableBinaryPredicateThe type of the function object that this binary_negate is the logical negation of.

Model of

Adaptable Binary Predicate

Type requirements

AdaptableBinaryPredicate must be a model of Adaptable Binary Predicate.

Public base classes

binary_function<AdaptableBinaryPredicate::first_argument_type, AdaptableBinaryPredicate::second_argument_type, bool>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: AdaptableBinaryPredicate::first_argument_type
second_argument_typeAdaptable Binary FunctionThe type of the second argument: AdaptableBinaryPredicate::second_argument_type
result_typeAdaptable Binary FunctionThe type of the result: bool
binary_negate(const AdaptableBinaryPredicate& pred)binary_negateSee below.
template <class AdaptableBinaryPredicate> binary_negate<AdaptableBinaryPredicate> not2(const AdaptableBinaryPredicate& pred);binary_negateSee below.

New members

These members are not defined in the Adaptable Binary Predicate requirements, but are specific to binary_negate.

MemberDescription
binary_negate(const AdaptableBinaryPredicate& pred)The constructor. Creates a binary_negate<AdaptableBinaryPredicate> whose underlying predicate is pred.
template <class AdaptableBinaryPredicate> binary_negate<AdaptableBinaryPredicate> not2(const AdaptableBinaryPredicate& pred);If p is of type AdaptableBinaryPredicate then not2(p) is equivalent to binary_negate<AdaptableBinaryPredicate>(p), but more convenient. This is a global function, not a member function.

See also

The function object overview, AdaptablePredicate, Predicate, unary_negate, unary_compose, binary_compose

unary_compose<AdaptableUnaryFunction1,AdaptableUnaryFunction2>

Categories: functors, adaptors

Component type: type

Description

Unary_compose is a function object adaptor. If f and g are both Adaptable Unary Functions, and if g's return type is convertible to f's argument type, then unary_compose can be used to create a function object h such that h(x) is the same as f(g(x)). [1] As with other function object adaptors, the easiest way to create a unary_compose is to use the helper function compose1. It is possible to call unary_compose's constructor directly, but there is usually no reason to do so.

Example

Calculates the negative of the sines of the elements in a vector, where the elements are angles measured in degrees. Since the C library function sin takes its arguments in radians, this operation is the composition of three operations: negation, sin, and the conversion of degrees to radians.

vector<double> angles;

vector<double> sines;

const double pi = 3.14159265358979323846;

assert(sines.size() >= angles.size());

transform(angles.begin(), angles.end(), sines.begin(), compose1(negate<double>(), compose1(ptr_fun(sin), bind2nd(multiplies<double>(), pi / 180.))));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. The unary_compose class is an SGI extension; it is not part of the C++ standard.

Template parameters

ParameterDescription
AdaptableUnaryFunction1The type of the first operand in the function composition operation. That is, if the composition is written f o g [1], then AdaptableUnaryFunction1 is the type of the function object f.
AdaptableUnaryFunction2The type of the second operand in the function composition operation. That is, if the composition is written f o g [1], then AdaptableUnaryFunction1 is the type of the function object g.

Model of

Adaptable Unary Function

Type requirements

AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must both be models of Adaptable Unary Function. AdaptableUnaryFunction2::result_type must be convertible to AdaptableUnaryFunction1::argument_type.

Public base classes

unary_function<AdaptableUnaryFunction2::argument_type, AdaptableUnaryFunction1::result_type>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the function object's argument: AdaptableUnaryFunction2::argument_type.
result_typeAdaptable Unary FunctionThe type of the result: AdaptableUnaryFunction1::result_type
unary_compose(const AdaptableUnaryFunction1& f, const AdaptableUnaryFunction2& g);unary_composeSee below.
template <class AdaptableUnaryFunction1, class AdaptableUnaryFunction2> unary_compose<AdaptableUnaryFunction1, AdaptableUnaryFunction2> compose1(const AdaptableUnaryFunction1& op1, const AdaptableUnaryFunction2& op2);unary_composeSee below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to unary_compose.

MemberDescription
unary_compose(const AdaptableUnaryFunction1& f, const AdaptableUnaryFunction2& g);The constructor. Constructs a unary_compose object that represents the function object f o g. [1]
template <class AdaptableUnaryFunction1, class AdaptableUnaryFunction2> unary_compose<AdaptableUnaryFunction1, AdaptableUnaryFunction2> compose1(const AdaptableUnaryFunction1& op1, const AdaptableUnaryFunction2& op2);Creates a unary_compose object. If f and g are, respectively, of classes AdaptableUnaryFunction1 and AdaptableUnaryFunction2, then compose1(f, g) is equivalent to unary_compose<AdaptableUnaryFunction1, AdaptableUnaryFunction2>(f, g), but is more convenient. This is a global function, not a member function.

Notes

[1] This operation is called function composition, hence the name unary_compose. It is often represented in mathematics as the operation f o g, where f o g is a function such that (f o g)(x) == f(g(x)). Function composition is a very important concept in algebra. It is also extremely important as a method of building software components out of other components, because it makes it possible to construct arbitrarily complicated function objects out of simple ones.

See also

The function object overview, binary_compose, binder1st, binder2nd.

binary_compose<AdaptableBinaryFunction,AdaptableUnaryFunction1,AdaptableUnaryFunction2>

Categories: functors, adaptors

Component type: type

Description

Binary_compose is a function object adaptor. If f is an Adaptable Binary Function and g1 and g2 are both Adaptable Unary Functions, and if g1's and g2's return types are convertible to f's argument types, then binary_compose can be used to create a function object h such that h(x) is the same as f(g1(x), g2(x)). [1] [2]

Example

Finds the first element in a list that lies in the range from 1 to 10.

list<int> L;

list<int>::iterator in_range = find_if(L.begin(), L.end(), compose2(logical_and<bool>(), bind2nd(greater_equal<int>(), 1), bind2nd(less_equal<int>(), 10)));

assert(in_range == L.end() || (*in_range >= 1 && *in_range <= 10));

Computes sin(x)/(x + DBL_MIN) for each element of a range.

transform(first, last, first, compose2(divides<double>(), ptr_fun(sin), bind2nd(plus<double>(), DBL_MIN)));

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h. The binary_compose class is an SGI extension; it is not part of the C++ standard.

Template parameters

ParameterDescription
AdaptableBinaryFunctionThe type of the "outer" function in the function composition operation. That is, if the binary_compose is a function object h such that h(x) = f(g1(x), g2(x)), then AdaptableBinaryFunction is the type of f.
AdaptableUnaryFunction1The type of the first "inner" function in the function composition operation. That is, if the binary_compose is a function object h such that h(x) = f(g1(x), g2(x)), then AdaptableBinaryFunction is the type of g1.
AdaptableUnaryFunction2The type of the second "inner" function in the function composition operation. That is, if the binary_compose is a function object h such that h(x) = f(g1(x), g2(x)), then AdaptableBinaryFunction is the type of g2.

Model of

Adaptable Unary Function

Type requirements

AdaptableBinaryFunction must be a model of Adaptable Binary Function. AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must both be models of Adaptable Unary Function. The argument types of AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must be convertible to each other. The result types of AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must be convertible, respectively, to the first and second argument types of AdaptableBinaryFunction.

Public base classes

unary_function<AdaptableUnaryFunction1::argument_type, AdaptableBinaryFunction::result_type>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the function object's argument: AdaptableUnaryFunction::argument_type.
result_typeAdaptable Unary FunctionThe type of the result: AdaptableBinaryFunction::result_type
binary_compose(const AdaptableBinaryFunction& f, const AdaptableUnaryFunction1& g1, const AdaptableUnaryFunction1& g2);binary_composeSee below.
template <class AdaptableBinaryFunction, class AdaptableUnaryFunction1, class AdaptableUnaryFunction 2> binary_compose<AdaptableBinaryFunction, AdaptableUnaryFunction1, AdaptableUnaryFunction2> compose2(const AdaptableBinaryFunction&, const AdaptableUnaryFunction1&, const AdaptableUnaryFunction2&);binary_composeSee below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to binary_compose.

MemberDescription
binary_compose(const AdaptableBinaryFunction& f, const AdaptableUnaryFunction1& g1, const AdaptableUnaryFunction1& g2);The constructor. Constructs a binary_compose object such that calling that object with the argument x returns f(g1(x), g2(x)).
template <class AdaptableBinaryFunction, class AdaptableUnaryFunction1, class AdaptableUnaryFunction2> binary_compose<AdaptableBinaryFunction, AdaptableUnaryFunction1, AdaptableUnaryFunction2> compose2(const AdaptableBinaryFunction&, const AdaptableUnaryFunction1&, const AdaptableUnaryFunction2&);Creates a binary_compose object. If f, g, and g2 are, respectively, of classes AdaptableBinaryFunction, AdaptableUnaryFunction1, and AdaptableUnaryFunction2, then compose2(f, g1, g2) is equivalent to binary_compose<AdaptableBinaryFunction, AdaptableUnaryFunction1, AdaptableUnaryFunction2>(f, g1, g2), but is more convenient. This is a global function, not a member function.

Notes

[1] This is a form of function composition. The unary_compose adaptor allows composition of Adaptable Unary Functions; note, however, that once binary functions are introduced, there are several possible patterns of function composition. The binary_compose allows you to form a unary function by putting together two unary functions and a binary function, but you could also, for example, imagine putting together two unary functions and a binary function to form a binary function. In that case, f, g1, and g2 would be combined into a function object h such that h(x,y) = f(g1(x), g2(y)).

See also

The function object overview, unary_compose, binder1st, binder2nd.

Member function adaptors

mem_fun_t<Result, X>

Categories: functors, adaptors

Component type: type

Description

Mem_fun_t is an adaptor for member functions. If X is some class with a member function Result X::f() (that is, a member function that takes no arguments and that returns a value of type Result [1]), then a mem_fun_t<Result, X> is a function object adaptor that makes it possible to call f() as if it were an ordinary function instead of a member function.

Mem_fun_t<Result, X>'s constructor takes a pointer to one of X's member functions. Then, like all function objects, mem_fun_t has an operator() that allows the mem_fun_t to be invoked with ordinary function call syntax. In this case, mem_fun_t's operator() takes an argument of type X*.

If F is a mem_fun_t that was constructed to use the member function X::f, and if x is a pointer of type X* , then the expression F(x) is equivalent to the expression x->f(). The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.

Mem_fun_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references.

As with many other adaptors, it is usually inconvenient to use mem_fun_t's constructor directly. It is usually better to use the helper function mem_fun instead.

Example

struct B {

 virtual void print() = 0;

};

struct D1 : public B {

 void print() { cout << "I'm a D1" << endl; }

};

struct D2 : public B {

 void print() { cout << "I'm a D2" << endl; }

};

int main() {

 vector<B*> V;

 V.push_back(new D1);

 V.push_back(new D2);

 V.push_back(new D2);

 V.push_back(new D1);

 for_each(V.begin(), V.end(), mem_fun(&B::print));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
ResultThe member function's return type.
XThe class whose member function the mem_fun_t invokes.

Model of

Adaptable Unary Function

Type requirements

• X has at least one member function that takes no arguments and that returns a value of type Result. [1]

Public base classes

unary_function<X*, Result>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the argument: X*
result_typeAdaptable Unary FunctionThe type of the result: Result
Result operator()(X* x) constUnary Function Functioncall operator. Invokes x->f(), where f is the member function that was passed to the constructor.
explicit mem_fun_t(Result (X::*f)())mem_fun_tSee below.
template <class Result, class X> mem_fun_t<Result, X> mem_fun(Result (X::*f)());mem_fun_tSee below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to mem_fun_t.

MemberDescription
explicit mem_fun_t(Result (X::*f)())The constructor. Creates a mem_fun_t that calls the member function f.
template <class Result, class X> mem_fun_t<Result, X> mem_fun(Result (X::*f)());If f if of type Result (X::*) then mem_fun(f) is the same as mem_fun_t<Result, X>(f), but is more convenient. This is a global function, not a member function.

Notes

[1] The type Result is permitted to be void. That is, this adaptor may be used for functions that return no value. However, this presents implementation difficulties. According to the draft C++ standard, it is possible to return from a void function by writing return void instead of just return. At present, however (early 1998), very few compilers support that feature. As a substitute, then, mem_fun_t uses partial specialization to support void member functions. If your compiler has not implemented partial specialization, then you will not be able to use mem_fun_t with member functions whose return type is void.

See also

mem_fun_ref_t, mem_fun1_t, mem_fun1_ref_t

mem_fun_ref_t<Result, X>

Categories: functors, adaptors

Component type: type

Description

Mem_fun_ref_t is an adaptor for member functions. If X is some class with a member function Result X::f() (that is, a member function that takes no arguments and that returns a value of type Result [1]), then a mem_fun_ref_t<Result, X> is a function object adaptor that makes it possible to call f() as if it were an ordinary function instead of a member function.

mem_fun_ref_t<Result, X>'s constructor takes a pointer to one of X's member functions. Then, like all function objects, mem_fun_ref_t has an operator() that allows the mem_fun_ref_t to be invoked with ordinary function call syntax. In this case, mem_fun_ref_t's operator() takes an argument of type X&.

If F is a mem_fun_ref_t that was constructed to use the member function X::f, and if x is of type X, then the expression F(x) is equivalent to the expression x.f(). The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.

Mem_fun_ref_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references. In fact, though, mem_fun_ref_t is usually not as useful as mem_fun_t. The difference between the two is that mem_fun_t's argument is a pointer to an object while mem_fun_ref_t's argument is a reference to an object. References, unlike pointers, can't be stored in STL containers: pointers are objects in their own right, but references are merely aliases.

As with many other adaptors, it is usually inconvenient to use mem_fun_ref_t's constructor directly. It is usually better to use the helper function mem_fun_ref instead.

Example

struct B {

 virtual void print() = 0;

};

struct D1 : public B {

 void print() { cout << "I'm a D1" << endl; }

};

struct D2 : public B {

 void print() { cout << "I'm a D2" << endl; }

};

int main() {

 vector<D1> V;

 V.push_back(D1());

 V.push_back(D1());

 for_each(V.begin(), V.end(), mem_fun_ref(B::print));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
ResultThe member function's return type.
XThe class whose member function the mem_fun_ref_t invokes.

Model of

Adaptable Unary Function

Type requirements

• X has at least one member function that takes no arguments and that returns a value of type Result. [1]

Public base classes

unary_function<X, Result>

Members

MemberWhere definedDescription
argument_typeAdaptable Unary FunctionThe type of the argument: X
result_typeAdaptable Unary FunctionThe type of the result: Result
Result operator()(X& x) constUnary FunctionFunction call operator. Invokes x.f(), where f is the member function that was passed to the constructor.
explicit mem_fun_ref_t(Result (X::*f)())mem_fun_ref_tSee below.
template <class Result, class X> mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)());mem_fun_ref_tSee below.

New members

These members are not defined in the Adaptable Unary Function requirements, but are specific to mem_fun_ref_t.

MemberDescription
explicit mem_fun_ref_t(Result (X::*f)())The constructor. Creates a mem_fun_ref_t that calls the member function f.
template <class Result, class X> mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)());If f is of type Result (X::*)() then mem_fun_ref(f) is the same as mem_fun_ref_t<Result, X>(f), but is more convenient. This is a global function, not a member function.

Notes

[1] The type Result is permitted to be void. That is, this adaptor may be used for functions that return no value. However, this presents implementation difficulties. According to the draft C++ standard, it is possible to return from a void function by writing return void instead of just return. At present, however (early 1998), very few compilers support that feature. As a substitute, then, mem_fun_ref_t uses partial specialization to support void member functions. If your compiler has not implemented partial specialization, then you will not be able to use mem_fun_ref_t with member functions whose return type is void.

See also

mem_fun_t, mem_fun1_t, mem_fun1_ref_t

mem_fun1_t<Result, X, Arg>

Categories: functors, adaptors

Component type: type

Description

Mem_fun1_t is an adaptor for member functions. If X is some class with a member function Result X::f(Arg) (that is, a member function that takes one argument of type Arg and that returns a value of type Result [1]), then a mem_fun1_t<Result, X, Arg> is a function object adaptor that makes it possible to call f as if it were an ordinary function instead of a member function.

Mem_fun1_t<Result, X, Arg>'s constructor takes a pointer to one of X's member functions. Then, like all function objects, mem_fun1_t has an operator() that allows the mem_fun1_t to be invoked with ordinary function call syntax. In this case, mem_fun1_t's operator() takes two arguments; the first is of type X* and the second is of type Arg.

If F is a mem_fun1_t that was constructed to use the member function X::f, and if x is a pointer of type X* and a is a value of type Arg, then the expression F(x, a) is equivalent to the expression x->f(a). The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.

Mem_fun1_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references.

As with many other adaptors, it is usually inconvenient to use mem_fun1_t's constructor directly. It is usually better to use the helper function mem_fun [2] instead.

Example

struct Operation {

 virtual double eval(double) = 0;

};

struct Square : public Operation {

 double eval(double x) { return x * x; }

};

struct Negate : public Operation {

 double eval(double x) { return –x; }

};

int main() {

 vector<Operation*> operations;

 vector<double> operands;

 operations.push_back(new Square);

 operations.push_back(new Square);

 operations.push_back(new Negate);

 operations.push_back(new Negate);

 operations.push_back(new Square);

 operands.push_back(1);

 operands.push_back(2);

 operands.push_back(3);

 operands.push_back(4);

 operands.push_back(5);

 transform(operations.begin(), operations.end(), operands.begin(), ostream_iterator<double>(cout, "\n"), mem_fun(Operation::eval));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
ResultThe member function's return type.
XThe class whose member function the mem_fun1_t invokes.
ArgThe member function's argument type.

Model of

Adaptable Binary Function

Type requirements

• X has at least one member function that takes a single argument of type Arg and that returns a value of type Result. [1]

Public base classes

binary_function<X*, Arg, Result>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: X*
second_argument_typeAdaptable Binary FunctionThe type of the second argument: Arg
result_typeAdaptable Binary FunctionThe type of the result: Result
Result operator()(X* x, Arg a) constBinary FunctionFunction call operator. Invokes x->f(a), where f is the member function that was passed to the constructor.
explicit mem_fun1_t(Result (X::*f)(Arg))mem_fun1_tSee below.
template <class Result, class X, class Arg> mem_fun1_t<Result, X, Arg> mem_fun(Result (X::*f)(Arg)); [2]mem_fun1_tSee below.

New members

These members are not defined in the Adaptable Binary Function requirements, but are specific to mem_fun1_t.

MemberDescription
explicit mem_fun1_t(Result (X::*f)(Arg))The constructor. Creates a mem_fun1_t that calls the member function f.
template <class Result, class X, class Arg> mem_fun1_t<Result, X, Arg> mem_fun(Result (X::*f)(Arg)); [2]If f is of type Result (X::*)(Arg) then mem_fun(f) is the same as mem_fun1_t<Result, X, Arg>(f), but is more convenient. This is a global function, not a member function.

Notes

[1] The type Result is permitted to be void. That is, this adaptor may be used for functions that return no value. However, this presents implementation difficulties. According to the draft C++ standard, it is possible to return from a void function by writing return void instead of just return. At present, however (early 1998), very few compilers support that feature. As a substitute, then, mem_fun1_t uses partial specialization to support void member functions. If your compiler has not implemented partial specialization, then you will not be able to use mem_fun1_t with member functions whose return type is void.

[2] This helper function was called mem_fun1 in drafts of the C++ standard, but it is called mem_fun in the final standard. This implementation provides both versions for backward compatibility, but mem_fun1 will be removed in a future release.

See also

mem_fun_t, mem_fun_ref_t, mem_fun1_ref_t

mem_fun1_ref_t<Result, X, Arg>

Categories: functors, adaptors

Component type: type

Description

Mem_fun1_ref_t is an adaptor for member functions. If X is some class with a member function Result X::f(Arg) (that is, a member function that takes one argument of type Arg and that returns a value of type Result [1]), then a mem_fun1_ref_t<Result, X, Arg> is a function object adaptor that makes it possible to call f as if it were an ordinary function instead of a member function.

Mem_fun1_ref_t<Result, X, Arg>'s constructor takes a pointer to one of X's member functions. Then, like all function objects, mem_fun1_ref_t has an operator() that allows the mem_fun1_ref_t to be invoked with ordinary function call syntax. In this case, mem_fun1_ref_t's operator() takes two arguments; the first is of type X and the second is of type Arg.

If F is a mem_fun1_ref_t that was constructed to use the member function X::f, and if x is an object of type X and a is a value of type Arg , then the expression F(x, a) is equivalent to the expression x.f(a). The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.

Mem_fun1_ref_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references. In fact, though, mem_fun1_ref_t is usually not as useful as mem_fun1_t. The difference between the two is that mem_fun1_t's first argument is a pointer to an object while mem_fun1_ref_t's argument is a reference to an object. References, unlike pointers, can't be stored in STL containers: pointers are objects in their own right, but references are merely aliases.

As with many other adaptors, it is usually inconvenient to use mem_fun1_ref_t's constructor directly. It is usually better to use the helper function mem_fun_ref [2] instead.

Example

Given a vector of vectors, extract one element from each vector.

int main() {

 int A1[5] = {1, 2, 3, 4, 5};

 int A2[5] = {1, 1, 2, 3, 5};

 int A3[5] = {1, 4, 1, 5, 9};

 vector<vector<int> > V;

 V.push_back(vector<int>(A1, A1 + 5));

 V.push_back(vector<int>(A2, A2 + 5));

 V.push_back(vector<int>(A3, A3 + 5));

 int indices[3] = {0, 2, 4};

 int& (vector<int>::*extract)(vector<int>::size_type);

 extract = vector<int>::operator[];

 transform (V.begin(), V.end(), indices, ostream_iterator<int>(cout, "\n"), mem_fun_ref(extract));

}

Definition

Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.

Template parameters

ParameterDescription
ResultThe member function's return type.
XThe class whose member function the mem_fun1_ref_t invokes.
ArgThe member function's argument type.

Model of

Adaptable Binary Function

Type requirements

X has at least one member function that takes a single argument of type Arg and that returns a value of type Result. [1]

Public base classes

binary_function<X, Arg, Result>

Members

MemberWhere definedDescription
first_argument_typeAdaptable Binary FunctionThe type of the first argument: X
second_argument_typeAdaptable Binary FunctionThe type of the second argument: Arg
result_typeAdaptable Binary FunctionThe type of the result: Result
Result operator()(X& x, Arg a) constBinary FunctionFunction call operator. Invokes x.f(a), where f is the member function that was passed to the constructor.
explicit mem_fun1_ref_t(Result (X::*f)(Arg))mem_fun1_ref_tSee below.
template <class Result, class X, class Arg> mem_fun1_ref_t<Result, X, Arg> mem_fun_ref(Result (X::*f)(Arg)); [2]mem_fun1_ref_tSee below.

New members

These members are not defined in the Adaptable Binary Function requirements, but are specific to mem_fun1_ref_t.

MemberDescription
explicit mem_fun1_ref_t(Result (X::*f)(Arg))The constructor. Creates a mem_fun1_ref_t that calls the member function f.
template <class Result, class X, class Arg> mem_fun1_ref_t<Result, X, Arg> mem_fun1_ref(Result (X::*f)(Arg)); [2]If f is of type Result (X::*)(Arg) then mem_fun_ref(f) is the same as mem_fun1_ref_t<Result, X, Arg>(f), but is more convenient. This is a global function, not a member function.

Notes

[1] The type Result is permitted to be void. That is, this adaptor may be used for functions that return no value. However, this presents implementation difficulties. According to the draft C++ standard, it is possible to return from a void function by writing return void instead of just return. At present, however (early 1998), very few compilers support that feature. As a substitute, then, mem_fun1_ref_t uses partial specialization to support void member functions. If your compiler has not implemented partial specialization, then you will not be able to use mem_fun1_ref_t with member functions whose return type is void.

[2] This helper function was called mem_fun1_ref in drafts of the C++ standard, but it is called mem_fun_ref in the final standard. This implementation provides both versions for backward compatibility, but mem_fun1_ref will be removed in a future release.

See also

mem_fun_t, mem_fun_ref_t, mem_fun1_t