In previous discussions have repeatedly flashed the term as a functor, but it acquires special relevance in relation to the algorithms. Now it's time to deal with this concept. Functor - an abbreviation of functional object, representing the structure, allows you to use the class object as a function of. In C ++, for the definition of the functor class to describe enough, which redefined the operation ().
Then, as a function of the object is formed, easy to show on this simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> #include <vector> using namespace std; class summator : private vector<int> { public: summator(const vector<int>& ini) { for (auto x : ini) this->push_back(x); } int operator()(bool even) { int sum = 0; auto i = begin(); if (even) i++; while (i < end()) { sum += *i++; if (i == end()) break; i++; } return sum; } }; int main(void) { setlocale(LC_ALL, "rus"); summator sums(vector<int>({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })); cout << "сумма чётных = " << sums(true) << endl << "сумма нечётных = " << sums(false) << endl; } |
Even from this simple example shows the following: operation () in the class may be overridden (accurately determine, because it has no default implementation) Random number, the type of parameters and return type (or even does not return value). Eventually:
The benefit is that the functor, what and). You can define when you create the object (before calling) using the constructor with parameters and b). It can create a temporary object only for the duration of the function call. This is illustrated by the example of the simplified integral calculator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <iostream> using namespace std; class calculate { char op; public: calculate( char op ) : op( op ) {} int operator()( int op1, int op2 ) { switch( op ) { case '+': return op1 + op2; case '-': return op1 - op2; case '*': return op1 * op2; case '/': return op1 / op2; case '%': return op1 % op2; case '^': { int ret = op1; while( op2-- > 1 ) ret *= op1; return ret; } default: cout << "неразрешённая операция" << endl; return 0; } } }; int main( int argc, char **argv, char **envp ) { setlocale(LC_ALL, "rus"); char oper; int op1, op2; do { cout << "выражение для вычисления (<op1><знак><op2>): " << flush; cin >> op1 >> oper >> op2; cout << op1 << ' ' << oper << ' ' << op2 << " = " << calculate( oper )( op1, op2 ) << endl; } while( true ); return 0; } |
Here, in a string cout << calculate( oper )( on 1, on 2 ) Actions are executed sequentially:
creates a temporary object class calculate constructor with a parameter oper;
method is executed for the object () (function call) with two parameters;
operation, to be executed in the function call, tt depends on the parameter oper, with which the object was constructed;
function call returns the result of the operation;
established temporary object, immediately thereafter destroyed (if he has been described destructor, it would have been called at this point);
And as a result we get:
But especially widespread use functors acquired algorithms STL, discussed earlier, when they are passed in the challenge as a parameter, instead of the function, defining action or predicate algorithm.
Why in the first example of a symbol : standing in front of the word public? Explain, you are welcome)
That is, before the private)
This is from the other C ++ Forum: classes, Area of visibility.
The example refers to the visibility private base class: access to the base class, there is only from the inside class summator.
Qualifier public more already defined within class, and refers to a member function, announced after: constructor and operator (). It shows that these functions are members of visible and can be used anywhere in the program.
In this example, all of this is not so important, but it is a separate and important part of the C ++ language.
t6d8rk