52942.fb2
Category: functors
Component type: overview
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().
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.
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());
• Generator
• Unary Function
• Binary Function
• Predicate
• Binary Predicate
• Adaptable Generator
• Adaptable Unary Function
• Adaptable Binary Function
• Adaptable Predicate
• Adaptable Binary Predicate
• 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
• compose1
• compose2
• not1
• not2
• bind1st
• bind2nd
• ptr_fun
[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.
Category: functors
Component type: concept
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.
Assignable
Result type | The type returned when the Generator is called |
F
A type that is a model of Generator
Result
The result type of F
f
Object of type F
The range of a Generator is the set of all possible value that it may return.
Name | Expression | Return type |
---|---|---|
Function call | f() | Result |
Name | Expression | Semantics | Postcondition |
---|---|---|---|
Function call | f() | Returns some value of type Result [1] | The return value is in f 's range. |
• Result (*)()
[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.
Function Object overview, Unary Function, Binary Function, Adaptable Generator
Category: functors
Component type: concept
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.
Assignable
Argument type | The type of the Unary Function's argument. |
Result type | The type returned when the Unary Function is called |
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
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.
Name | Expression | Return type |
---|---|---|
Function call | f(x) | Result |
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Function call | f(x) | x is in f's domain | Calls f with x as an argument, and returns a value of type Result [1] | The return value is in f's range |
• Result (*)(X)
[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.
Function Object overview, Generator, Binary Function Adaptable Unary Function
Category: functors
Component type: concept
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.
Assignable
First argument type | The type of the Binary Function's first argument. |
Second argument type | The type of the Binary Function's second argument. |
Result type | The type returned when the Binary Function is called |
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
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.
Name | Expression | Return type |
---|---|---|
Function call | f(x,y) | Result |
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Function call | f(x,y) | The ordered pair (x,y) is in f's domain | Calls f with x and y as arguments, and returns a value of type Result [1] | The return value is in f's range |
• Result (*)(X,Y)
[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.
Function Object overview, Generator, Unary Function, Adaptable Binary Function
Category: functors
Component type: concept
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.
Generator
Result type | F::result_type | The type returned when the Generator is called |
F
A type that is a model of Adaptable Generator
None, except for those defined by Generator
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;
};
[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.
Generator, Adaptable Unary Function, Adaptable Binary Function
Category: functors
Component type: concept
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.
Unary Function
Argument type | F::argument_type | The type of F's argument |
Result type | F::result_type | The type returned when the Unary Function is called |
F
A type that is a model of Unary Function
None, except for those defined by Unary Function
• negate
• identity
• pointer_to_unary_function
[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.
Unary Function, Adaptable Generator, Adaptable Binary Function
Category: functors
Component type: concept
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.
Binary Function
First argument type | F::first_argument_type | The type of F's first argument |
Second argument type | F::second_argument_type | The type of F's second argument |
Result type | F::result_type | The type returned when the Binary Function is called |
F
A type that is a model of Binary Function
None, except for those defined by Binary Function
• plus
• project1st
• pointer_to_binary_function
[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.
Binary Function, Adaptable Generator, Adaptable Unary Function
Category: functors
Component type: concept
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.
Unary Function
Result type | The type returned when the Predicate is called. The result type must be convertible to bool. |
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
Name | Expression | Return type |
---|---|---|
Function call | f(x) | Convertible to bool |
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Function call | f(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. |
• bool (*)(int)
Adaptable Predicate, Binary Predicate, Adaptable Binary Predicate
Category: functors
Component type: concept
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.
Binary Function
Result type | The type returned when the Binary Predicate is called. The result type must be convertible to bool. |
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
Name | Expression | Return type |
---|---|---|
Function call | f(x,y) | Convertible to bool |
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Function call | f(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. |
• bool (*)(int,int)
• equal_to
Predicate, Adaptable Predicate, Adaptable Binary Predicate
Category: functors
Component type: concept
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.
Predicate, Adaptable Unary Function
None, except for those associated with Predicate and Adaptable Unary Function.
None, except for those defined by the Predicate and Adaptable Unary Function requirements.
• logical_not
• unary_negate
Predicate, Binary Predicate, Adaptable Binary Predicate
Category: functors
Component type: concept
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.
Predicate, Adaptable Binary Function
None, except for those associated with Predicate and Adaptable Binary Function.
None, except for those defined by the Predicate and Adaptable Binary Function requirements.
• less
• equal_to
• logical_and
• logical_or
• binary_negate
Binary Predicate, Predicate, Adaptable Predicate
Category: functors
Component type: concept
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.
Binary Predicate
First argument type | The type of the Strict Weak Ordering's first argument. |
Second argument type | The type of the Strict Weak Ordering's second argument. The first argument type and second argument type must be the same. |
Result type | The type returned when the Strict Weak Ordering is called. The result type must be convertible to bool. |
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
• 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.
None, except for those defined in the Binary Predicate requirements.
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Function call | f(x, y) | The ordered pair (x,y) is in the domain of f | Returns true if x precedes y, and false otherwise | The result is either true or false |
Irreflexivity | f(x, x) must be false. |
Antisymmetry | f(x, y) implies !f(y, x) |
Transitivity | f(x, y) and f(y, z) imply f(x, z). |
Transitivity of equivalence | Equivalence (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] |
• less<int>
• less<double>
• greater<int>
• greater<double>
[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.
LessThan Comparable, less, Binary Predicate, function objects
Category: functors
Component type: concept
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]
Binary Function
Argument type | The type of the Monoid Operation's first argument and second argument, and also the type returned when the Monoid Operation is returned. |
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
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]
In addition to the expressions described in the Binary Function requirements, the following expressions must be valid.
Name | Expression | Return type |
---|---|---|
Function call | f(x, y) | T |
Identity element | identity_element(f)[3] | T |
Name | Expression | Precondition | Semantics |
---|---|---|---|
Function call | f(x, y) | x and y are in the domain of f. | Calls f with x and y as arguments. |
Identity element | identity_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. |
Associativity | For 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. |
• plus<int>
• multiplies<double>
[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.
Binary Function, plus, multiplies
Category: functors
Component type: concept
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]
Unary Function
Argument type | The type of the Random Number Generator's argument. This must be an integral type. |
Result type | The type returned when the Random Number Generator is called. It must be the same as the argument type. |
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
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.
None, except for those defined by Unary Function.
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Function call | f(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. |
Uniformity | In 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. |
[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.)
Category: functors
Component type: type
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.
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>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The function object's argument type and result type. |
Adaptable Binary Function, Default Constructible
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.
binary_function<T, T, T>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: T |
second_argument_type | Adaptable Binary Function | The type of the second argument: T |
result_type | Adaptable Binary Function | The type of the result: T |
T operator()(const T& x, const T& y) | Adaptable Binary Function | Function call operator. The return value is x + y. |
plus() | Default Constructible | The default constructor. |
All of plus's members are defined in the Adaptable Binary Function and Default Constructible requirements. Plus does not introduce any new members.
The Function Object overview, Adaptable Binary Function, binary_function, minus, multiplies, divides, modulus, negate
Category: functors
Component type: type
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.
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>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The function object's argument type and result type. |
Adaptable Binary Function, Default Constructible
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.
binary_function<T, T, T>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: T |
second_argument_type | Adaptable Binary Function | The type of the second argument: T |
result_type | Adaptable Binary Function | The type of the result: T |
T operator()(const T& x, const T& y) | Adaptable Binary Function | Function call operator. The return value is x - y. |
minus() | Default Constructible | The default constructor. |
All of minus's members are defined in the Adaptable Binary Function and Default Constructible requirements. Minus does not introduce any new members.
The Function Object overview, Adaptable Binary Function, binary_function, plus, multiplies, divides, modulus, negate
Category: functors
Component type: type
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.
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>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The function object's argument type and result type. |
Adaptable Binary Function, Default Constructible
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.
binary_function<T, T, T>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: T |
second_argument_type | Adaptable Binary Function | The type of the second argument: T |
result_type | Adaptable Binary Function | The type of the result: T |
T operator()(const T& x, const T& y) | Adaptable Binary Function | Function call operator. The return value is x * y. |
multiplies() [1] | Default Constructible | The default constructor. |
All of multiplies's members are defined in the Adaptable Binary Function and Default Constructible requirements. Multiplies does not introduce any new members.
[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>.
The Function Object overview, Adaptable Binary Function, binary_function, plus, minus, divides, modulus, negate
Category: functors
Component type: type
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.
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>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The function object's argument type and result type. |
Adaptable Binary Function, Default Constructible
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.
binary_function<T, T, T>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: T |
second_argument_type | Adaptable Binary Function | The type of the second argument: T |
result_type | Adaptable Binary Function | The type of the result: T |
T operator()(const T& x, const T& y) | Adaptable Binary Function | Function call operator. The return value is x / y. |
divides() | Default Constructible | The default constructor. |
All of divides's members are defined in the Adaptable Binary Function and Default Constructible requirements. Divides does not introduce any new members.
The Function Object overview, Adaptable Binary Function, binary_function, plus, minus, multiplies, modulus, negate
Category: functors
Component type: type
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.
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>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The function object's argument type and result type. |
Adaptable Binary Function, Default Constructible
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.
binary_function<T, T, T>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: T |
second_argument_type | Adaptable Binary Function | The type of the second argument: T |
result_type | Adaptable Binary Function | The type of the result: T |
T operator()(const T& x, const T& y) | Adaptable Binary Function | Function call operator. The return value is x % y. |
modulus() | Default Constructible | The default constructor. |
All of modulus's members are defined in the Adaptable Binary Function and Default Constructible requirements. Modulus does not introduce any new members.
The Function Object overview, Adaptable Binary Function, binary_function, plus, minus, multiplies, divides, negate
Category: functors
Component type: type
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.
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>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The function object's argument type and result type. |
Adaptable Unary Function, Default Constructible
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.
unary_function<T, T>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the second argument: T |
result_type | Adaptable Unary Function | The type of the result: T |
T operator()(const T& x) | Adaptable Unary Function | Function call operator. The return value is -x. |
negate() | Default Constructible | The default constructor. |
All of negate's members are defined in the Adaptable Unary Function and Default Constructible requirements. Negate does not introduce any new members.
The Function Object overview, Adaptable Unary Function, unary_function, plus, minus, multiplies, divides, modulus
Category: functors
Component type: type
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.
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));
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of equal_to's arguments. |
Adaptable Binary Predicate, DefaultConstructible
T is EqualityComparable.
binary_function<T, T, bool>.
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Predicate | The type of the first argument: T |
second_argument_type | Adaptable Binary Predicate | The type of the second argument: T |
result_type | Adaptable Binary Predicate | The type of the result: bool |
equal_to() | DefaultConstructible | The default constructor. |
bool operator()(const T& x, const T& y) | Binary Function | Function call operator. The return value is x == y. |
All of equal_to's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Equal_to does not introduce any new members.
The function object overview, Adaptable Binary Predicate, not_equal_to, greater, less, greater_equal, less_equal
Category: functors
Component type: type
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.
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);
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of not_equal_to's arguments. |
Adaptable Binary Predicate, DefaultConstructible
T is EqualityComparable.
binary_function<T, T, bool>.
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Predicate | The type of the first argument: T |
second_argument_type | Adaptable Binary Predicate | The type of the second argument: T |
result_type | Adaptable Binary Predicate | The type of the result: bool |
not_equal_to() | DefaultConstructible | The default constructor. |
bool operator()(const T& x, const T& y) | Binary Function | Function call operator. The return value is x != y. |
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.
The function object overview, Adaptable Binary Predicate, equal_to, greater, less, greater_equal, less_equal
Category: functors
Component type: type
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.
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);
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of less's arguments. |
Adaptable Binary Predicate, DefaultConstructible
T is LessThan Comparable.
binary_function<T, T, bool>.
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Predicate | The type of the first argument: T |
second_argument_type | Adaptable Binary Predicate | The type of the second argument: T |
result_type | Adaptable Binary Predicate | The type of the result: bool |
less() | DefaultConstructible | The default constructor. |
bool operator()(const T& x, const T& y) | Binary Function | Function call operator. The return value is x < y. |
All of less's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. less does not introduce any new members.
The function object overview, Strict Weak Ordering, Adaptable Binary Predicate, LessThan Comparable, equal_to, not_equal_to, greater, greater_equal, less_equal
Category: functors
Component type: type
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.
Sort a vector in descending order, rather than the default ascending order.
vector<int> V;
…
sort(V.begin(), V.end(), greater<int>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of greater's arguments. |
Adaptable Binary Predicate, DefaultConstructible
T is LessThan Comparable.
binary_function <T, T, bool>.
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Predicate | The type of the first argument: T |
second_argument_type | Adaptable Binary Predicate | The type of the second argument: T |
result_type | Adaptable Binary Predicate | The type of the result: bool |
greater() | DefaultConstructible | The default constructor. |
bool operator()(const T& x, const T& y) | Binary Function | Function call operator. The return value is x > y. |
All of greater's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Greater does not introduce any new members.
The function object overview, Adaptable Binary Predicate, LessThan Comparable, equal_to, not_equal_to, less, greater_equal, less_equal
Category: functors
Component type: type
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.
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);
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of less_equal's arguments. |
Adaptable Binary Predicate, DefaultConstructible
T is LessThan Comparable.
binary_function<T, T, bool>.
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Predicate | The type of the first argument: T |
second_argument_type | Adaptable Binary Predicate | The type of the second argument: T |
result_type | Adaptable Binary Predicate | The type of the result: bool |
less_equal() | DefaultConstructible | The default constructor. |
bool operator()(const T& x, const T& y) | Binary Function | Function call operator. The return value is x <= y. |
All of less_equal's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Less_equal does not introduce any new members.
The function object overview, Adaptable Binary Predicate, equal_to, not_equal_to, greater, less, greater_equal
Category: functors
Component type: type
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.
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);
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of greater_equal's arguments. |
Adaptable Binary Predicate, DefaultConstructible
T is LessThan Comparable.
binary_function<T, T, bool>.
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Predicate | The type of the first argument: T |
second_argument_type | Adaptable Binary Predicate | The type of the second argument: T |
result_type | Adaptable Binary Predicate | The type of the result: bool |
greater_equal() | DefaultConstructible | The default constructor. |
bool operator()(const T& x, const T& y) | Binary Function | Function call operator. The return value is x >= y. |
All of greater_equal's members are defined in the Adaptable Binary Predicate and DefaultConstructible requirements. Greater_equal does not introduce any new members.
The function object overview, Adaptable Binary Predicate, equal_to, not_equal_to, greater less, less_equal
Category: functors
Component type: type
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]
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));
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of logical_and's arguments |
Adaptable Binary Predicate, DefaultConstructible
T must be convertible to bool.
binary_function<T, T, bool>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: T |
second_argument_type | Adaptable Binary Function | The type of the second argument: T |
result_type | Adaptable Binary Function | The type of the result: bool |
bool operator()(const T& x, const T& y) const | Binary Function | Function call operator. The return value is x && y. |
logical_and() | Default Constructible | The default constructor. |
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.
[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.
The function object overview, logical_or, logical_not.
Category: functors
Component type: type
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]
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');
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of logical_or's arguments |
Adaptable Binary Predicate, DefaultConstructible
T must be convertible to bool.
binary_function<T, T, bool>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: T |
second_argument_type | Adaptable Binary Function | The type of the second argument: T |
result_type | Adaptable Binary Function | The type of the result: bool |
bool operator()(const T& x, const T& y) const | Binary Function Function | call operator. The return value is x || y. |
logical_or() | Default Constructible | The default constructor. |
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.
[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.
The function object overview, logical_and, logical_not.
Category: functors
Component type: type
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.
Transforms a vector of bool into its logical complement.
vector<bool> V;
…
transform(V.begin(), V.end(), V.begin(), logical_not<bool>());
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
T | The type of logical_not's argument |
Adaptable Predicate, DefaultConstructible
T must be convertible to bool.
unary_function<T, bool>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the second argument: T |
result_type | Adaptable Unary Function | The type of the result: bool |
bool operator()(const T& x) const | Unary Function Function call operator. | The return value is !x. |
logical_not() | Default Constructible | The default constructor. |
The function object overview, logical_or, logical_and.
Category: functors
Component type: type
Identity is a Unary Function that represents the identity function: it takes a single argument x, and returns x.
int main() {
int x = 137;
identity<int> id;
assert(x == id(x));
}
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.
Parameter | Description |
---|---|
T | The function object's argument type, and return type. [1] |
Adaptable Unary Function
None.
unary_function<T, T>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of identity's argument: T. |
result_type | Adaptable Unary Function | The type of the result: T. [1] |
const T& operator()(const T&) const | Adaptable Unary Function | Function call. The return value is simply the argument. |
All of identity's members are defined in the Adaptable Unary Function requirements. Identity does not introduce any new members.
[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.
The function object overview, select1st, select2nd, project1st, project2nd
Category: functors
Component type: type
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.
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()));
}
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.
Parameter | Description |
---|---|
Arg1 | project1st's first argument type, and its result type. |
Arg2 | project1st 's second argument type. |
Adaptable Binary Function
None.
binary_function<Arg1, Arg2, Arg1>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of project1st's first argument: Arg1 |
second_argument_type | Adaptable Binary Function | The type of project1st's second argument: Arg2 |
result_type | Adaptable Binary Function | The type of the result: Arg1. |
Arg1 operator()(const Arg1& x, const Arg2&) const | Adaptable Binary Function | Function call. The return value is x. |
All of project1st's members are defined in the Adaptable Binary Function requirements. project1st does not introduce any new members.
Function objects, identity, project2nd, select1st, select2nd
Category: functors
Component type: type
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.
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()));
}
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.
Parameter | Description |
---|---|
Arg1 | project2nd's first argument type. |
Arg2 | project2nd's second argument type, and its result type. |
Adaptable Binary Function
None.
binary_function<Arg1, Arg2, Arg2>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of project2nd's first argument: Arg1 |
second_argument_type | Adaptable Binary Function | The type of project2nd's second argument: Arg2 |
result_type | Adaptable Binary Function | The type of the result: Arg2. |
Arg1 operator()(const Arg1&, const Arg2& y) const | Adaptable Binary Function | Function call. The return value is y. |
All of project2nd's members are defined in the Adaptable Binary Function requirements. project2nd does not introduce any new members.
Function objects, identity, project1st, select1st, select2nd
Category: functors
Component type: type
Select1st is a function object that takes a single argument, a pair [1], and returns the pair's first element.
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.
}
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.
Parameter | Description |
---|---|
Pair | The function object's argument type. |
Adaptable Unary Function
There exist some types U and V such that Pair provides the same interface as a pair<U,V>. [1]
unary_function<Pair, Pair::first_type>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of select1st 's argument: Pair |
result_type | Adaptable Unary Function The type of the result: | Pair::first_type |
const Pair::first_type& operator()(const Pair& p) const | Adaptable Unary Function | Function call. The return value is p.first. |
All of select1st's members are defined in the Adaptable Unary Function requirements. Select1st does not introduce any new members.
[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.
identity, select2nd, project1st, project2nd
Category: functors
Component type: type
Select2nd is a function object that takes a single argument, a pair [1], and returns the pair's second element.
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
}
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.
Parameter | Description |
---|---|
Pair | The function object's argument type. |
Adaptable Unary Function
There exist some types U and V such that Pair provides the same interface as a pair<U,V>. [1]
unary_function<Pair, Pair::second_type>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of select2nd's argument: Pair |
result_type | Adaptable Unary Function | The type of the result: Pair::second_type |
const Pair::second_type& operator()(const Pair& p) const | Adaptable Unary Function | Function call. The return value is p.second. |
All of select2nd's members are defined in the Adaptable Unary Function requirements. Select2nd does not introduce any new members.
[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.
identity, select1st, project1st, project2nd
Category: functors
Component type: type
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.
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
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.
None.
Random Number Generator, Adaptable Unary Function
None.
unary_function<unsigned int, unsigned int>
Parameter | Description | Default |
---|---|---|
argument_type | Adaptable Unary Function | The type of a subtractive_rng's argument: unsigned int. |
result_type | Adaptable Unary Function | The type of the result: unsigned int. |
subtractive_rng(unsigned int seed) | subtractive_rng | See below. |
subtractive_rng() | subtractive_rng | See below. |
unsigned int operator()(unsigned int N) | Adaptable Unary Function | Function call. Returns a pseudo-random number in the range [0, N). |
void initialize(unsigned int seed) | subtractive_rng | See below. |
These members are not defined in the Adaptable Unary Function requirements, but are specific to subtractive_rng.
Member | Description |
---|---|
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. |
[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.
Random Number Generator
Categories: functors, adaptors
Component type: type
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.
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);
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
AdaptableBinaryFunction | The type of the binary function whose first argument is being bound to a constant. |
Adaptable Unary Function
AdaptableBinaryFunction must be a model of Adaptable Binary Function.
unary_function<AdaptableBinaryFunction::second_argument_type, AdaptableBinaryFunction::result_type>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the function object's argument, which is AdaptableBinaryFunction::second_argument_type |
result_type | Adaptable Unary Function | The type of the result: AdaptableBinaryFunction::result_type |
result_type operator()(const argument_type& x) const | Adaptable Unary Function | Function 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) | binder1st | See below |
template <class AdaptableBinaryFunction, class T> binder1st<AdaptableBinaryFunction> bind1st(const AdaptableBinaryFunction& F, const T& c); | binder1st | See below |
These members are not defined in the Adaptable Unary Function requirements, but are specific to binder1st.
Member | Description |
---|---|
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. |
[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.
The function object overview, binder2nd, Adaptable Unary Function, Adaptable Binary Function
Categories: functors, adaptors
Component type: type
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.
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);
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
AdaptableBinaryFunction | The type of the binary function whose second argument is being bound to a constant. |
Adaptable Unary Function
AdaptableBinaryFunction must be a model of Adaptable Binary Function.
unary_function<AdaptableBinaryFunction::first_argument_type, AdaptableBinaryFunction::result_type>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the function object's argument, which is AdaptableBinaryFunction::first_argument_type |
result_type | Adaptable Unary Function | The type of the result: AdaptableBinaryFunction::result_type |
result_type operator()(const argument_type& x) const | Adaptable Unary Function | Function 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) | binder2nd | See below |
template <class AdaptableBinaryFunction, class T> binder2nd<AdaptableBinaryFunction> bind2nd(const AdaptableBinaryFunction& F, const T& c); | binder2nd | See below |
These members are not defined in the Adaptable Unary Function requirements, but are specific to binder2nd.
Member | Description |
---|---|
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. |
[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.
The function object overview, binder1st, Adaptable Unary Function, Adaptable Binary Function
Categories: functors, adaptors
Component type: function
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));
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.
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
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.
See the examples in the discussions of pointer_to_unary_function and pointer_to_binary_function.
Function Objects, pointer_to_unary_function, pointer_to_binary_function, Adaptable Unary Function, Adaptable Binary Function
Categories: functors, adaptors
Component type: type
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.
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)));
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Arg | The function object's argument type |
Result | The function object's result type |
Adaptable Unary Function
• Arg is Assignable.
• Result is Assignable.
unary_function<Arg, Result>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the function object's argument: Arg. |
result_type | Adaptable Unary Function | The type of the result: Result |
result_type operator()(argument_type x) | Unary Function | Function call operator. |
pointer_to_unary_function(Result (*f)(Arg)) | pointer_to_unary_function | See below. |
pointer_to_unary_function() | pointer_to_unary_function | See below. |
template <class Arg, class Result> pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg)); | pointer_to_unary_function | See below. |
These members are not defined in the Adaptable Unary Function requirements, but are specific to pointer_to_unary_function.
Member | Description |
---|---|
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. |
pointer_to_binary_function, ptr_fun, Adaptable Unary Function
Categories: functors, adaptors
Component type: type
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.
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")));
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Arg1 | The function object's first argument type |
Arg2 | The function object's second argument type |
Result | The function object's result type |
Adaptable Binary Function
Arg1 is Assignable.
Arg2 is Assignable.
Result is Assignable.
binary_function<Arg1, Arg2, Result>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: Arg1. |
second_argument_type | Adaptable Binary Function | The type of the second argument: Arg2 |
result_type | Adaptable Binary Function | The type of the result: Result |
Result operator()(Arg1 x, Arg2 y) | Binary Function | Function call operator. |
pointer_to_binary_function(Result (*f)(Arg1, Arg2)) | pointer_to_binary_function | See below. |
pointer_to_binary_function() | pointer_to_binary_function | See below. |
template <class Arg1, class Arg2, class Result> pointer_to_unary_function<Arg1, Arg2, Result> ptr_fun(Result (*x)(Arg1, Arg2)); | pointer_to_binary_function | See below. |
These members are not defined in the Adaptable Binary Function requirements, but are specific to pointer_to_binary_function.
Member | Description |
---|---|
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. |
pointer_to_unary_function, ptr_fun, Adaptable Binary Function
Categories: functors, adaptors
Component type: type
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.
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));
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
AdaptablePredicate | The type of the function object that this unary_negate is the logical negation of. |
Adaptable Predicate
AdaptablePredicate must be a model of Adaptable Predicate.
unary_function<AdaptablePredicate::argument_type, bool>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the argument: AdaptablePredicate::argument_type |
result_type | Adaptable Unary Function | The type of the result: bool |
bool operator()(argument_type) | Unary Function | Function call operator. |
unary_negate(const AdaptablePredicate& pred) | unary_negate | See below. |
template <class AdaptablePredicate> unary_negate<AdaptablePredicate> not1(const AdaptablePredicate& pred); | unary_negate | See below. |
These members are not defined in the Adaptable Predicate requirements, but are specific to unary_negate.
Member | Description |
---|---|
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. |
[1] Strictly speaking, unary_negate is redundant. It can be constructed using the function object logical_not and the adaptor unary_compose.
The function object overview, Adaptable Predicate, Predicate, binary_negate, unary_compose, binary_compose
Categories: functors, adaptors
Component type: type
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.
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'));
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
AdaptableBinaryPredicate | The type of the function object that this binary_negate is the logical negation of. |
Adaptable Binary Predicate
AdaptableBinaryPredicate must be a model of Adaptable Binary Predicate.
binary_function<AdaptableBinaryPredicate::first_argument_type, AdaptableBinaryPredicate::second_argument_type, bool>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: AdaptableBinaryPredicate::first_argument_type |
second_argument_type | Adaptable Binary Function | The type of the second argument: AdaptableBinaryPredicate::second_argument_type |
result_type | Adaptable Binary Function | The type of the result: bool |
binary_negate(const AdaptableBinaryPredicate& pred) | binary_negate | See below. |
template <class AdaptableBinaryPredicate> binary_negate<AdaptableBinaryPredicate> not2(const AdaptableBinaryPredicate& pred); | binary_negate | See below. |
These members are not defined in the Adaptable Binary Predicate requirements, but are specific to binary_negate.
Member | Description |
---|---|
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. |
The function object overview, AdaptablePredicate, Predicate, unary_negate, unary_compose, binary_compose
Categories: functors, adaptors
Component type: type
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.
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.))));
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.
Parameter | Description |
---|---|
AdaptableUnaryFunction1 | The 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. |
AdaptableUnaryFunction2 | The 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. |
Adaptable Unary Function
AdaptableUnaryFunction1 and AdaptableUnaryFunction2 must both be models of Adaptable Unary Function. AdaptableUnaryFunction2::result_type must be convertible to AdaptableUnaryFunction1::argument_type.
unary_function<AdaptableUnaryFunction2::argument_type, AdaptableUnaryFunction1::result_type>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the function object's argument: AdaptableUnaryFunction2::argument_type. |
result_type | Adaptable Unary Function | The type of the result: AdaptableUnaryFunction1::result_type |
unary_compose(const AdaptableUnaryFunction1& f, const AdaptableUnaryFunction2& g); | unary_compose | See below. |
template <class AdaptableUnaryFunction1, class AdaptableUnaryFunction2> unary_compose<AdaptableUnaryFunction1, AdaptableUnaryFunction2> compose1(const AdaptableUnaryFunction1& op1, const AdaptableUnaryFunction2& op2); | unary_compose | See below. |
These members are not defined in the Adaptable Unary Function requirements, but are specific to unary_compose.
Member | Description |
---|---|
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. |
[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.
The function object overview, binary_compose, binder1st, binder2nd.
Categories: functors, adaptors
Component type: type
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]
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)));
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.
Parameter | Description |
---|---|
AdaptableBinaryFunction | The 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. |
AdaptableUnaryFunction1 | The 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. |
AdaptableUnaryFunction2 | The 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. |
Adaptable Unary Function
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.
unary_function<AdaptableUnaryFunction1::argument_type, AdaptableBinaryFunction::result_type>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the function object's argument: AdaptableUnaryFunction::argument_type. |
result_type | Adaptable Unary Function | The type of the result: AdaptableBinaryFunction::result_type |
binary_compose(const AdaptableBinaryFunction& f, const AdaptableUnaryFunction1& g1, const AdaptableUnaryFunction1& g2); | binary_compose | See below. |
template <class AdaptableBinaryFunction, class AdaptableUnaryFunction1, class AdaptableUnaryFunction 2> binary_compose<AdaptableBinaryFunction, AdaptableUnaryFunction1, AdaptableUnaryFunction2> compose2(const AdaptableBinaryFunction&, const AdaptableUnaryFunction1&, const AdaptableUnaryFunction2&); | binary_compose | See below. |
These members are not defined in the Adaptable Unary Function requirements, but are specific to binary_compose.
Member | Description |
---|---|
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. |
[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)).
The function object overview, unary_compose, binder1st, binder2nd.
Categories: functors, adaptors
Component type: type
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.
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));
}
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Result | The member function's return type. |
X | The class whose member function the mem_fun_t invokes. |
Adaptable Unary Function
• X has at least one member function that takes no arguments and that returns a value of type Result. [1]
unary_function<X*, Result>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the argument: X* |
result_type | Adaptable Unary Function | The type of the result: Result |
Result operator()(X* x) const | Unary Function Function | call 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_t | See below. |
template <class Result, class X> mem_fun_t<Result, X> mem_fun(Result (X::*f)()); | mem_fun_t | See below. |
These members are not defined in the Adaptable Unary Function requirements, but are specific to mem_fun_t.
Member | Description |
---|---|
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. |
[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.
mem_fun_ref_t, mem_fun1_t, mem_fun1_ref_t
Categories: functors, adaptors
Component type: type
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.
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));
}
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Result | The member function's return type. |
X | The class whose member function the mem_fun_ref_t invokes. |
Adaptable Unary Function
• X has at least one member function that takes no arguments and that returns a value of type Result. [1]
unary_function<X, Result>
Member | Where defined | Description |
---|---|---|
argument_type | Adaptable Unary Function | The type of the argument: X |
result_type | Adaptable Unary Function | The type of the result: Result |
Result operator()(X& x) const | Unary Function | Function 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_t | See below. |
template <class Result, class X> mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)()); | mem_fun_ref_t | See below. |
These members are not defined in the Adaptable Unary Function requirements, but are specific to mem_fun_ref_t.
Member | Description |
---|---|
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. |
[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.
mem_fun_t, mem_fun1_t, mem_fun1_ref_t
Categories: functors, adaptors
Component type: type
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.
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));
}
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Result | The member function's return type. |
X | The class whose member function the mem_fun1_t invokes. |
Arg | The member function's argument type. |
Adaptable Binary Function
• X has at least one member function that takes a single argument of type Arg and that returns a value of type Result. [1]
binary_function<X*, Arg, Result>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: X* |
second_argument_type | Adaptable Binary Function | The type of the second argument: Arg |
result_type | Adaptable Binary Function | The type of the result: Result |
Result operator()(X* x, Arg a) const | Binary Function | Function 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_t | See below. |
template <class Result, class X, class Arg> mem_fun1_t<Result, X, Arg> mem_fun(Result (X::*f)(Arg)); [2] | mem_fun1_t | See below. |
These members are not defined in the Adaptable Binary Function requirements, but are specific to mem_fun1_t.
Member | Description |
---|---|
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. |
[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.
mem_fun_t, mem_fun_ref_t, mem_fun1_ref_t
Categories: functors, adaptors
Component type: type
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.
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));
}
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Result | The member function's return type. |
X | The class whose member function the mem_fun1_ref_t invokes. |
Arg | The member function's argument type. |
Adaptable Binary Function
X has at least one member function that takes a single argument of type Arg and that returns a value of type Result. [1]
binary_function<X, Arg, Result>
Member | Where defined | Description |
---|---|---|
first_argument_type | Adaptable Binary Function | The type of the first argument: X |
second_argument_type | Adaptable Binary Function | The type of the second argument: Arg |
result_type | Adaptable Binary Function | The type of the result: Result |
Result operator()(X& x, Arg a) const | Binary Function | Function 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_t | See 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_t | See below. |
These members are not defined in the Adaptable Binary Function requirements, but are specific to mem_fun1_ref_t.
Member | Description |
---|---|
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. |
[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.
mem_fun_t, mem_fun_ref_t, mem_fun1_t