1. Function imitation
A function is also called a function object. An object of the function nature is to input some parameters, perform some operations on the parameters, and then return a value. To make the behavior similar to a function, you must customize the function call operator () in the category definition ().
Similar functions include the following types: arithmetic functions (plus <t>, minus <t>) relational operation functions (similar _to <t>, less <t>) logical operation class imitation functions (logical_and <t>, logical_or <t>, logical_not <t>) are the same, select, and project.
1.1 key to mating
It can be expanded to another class based on the original class. For this purpose, the imitation function defines two classes, representing the unary and binary imitation functions. Select a class to inherit from any subsequent function. The Code is as follows:
Unary Functions
Template <class Arg, classresult>
Struct unary_function
{
Typedef Arg argument_type;
Typedef result result_type;
};
Template <class T>
Struct negate: publicunary_function <t, t>
{
T operator () (const T & X) const {return-X ;}
};
Binary Functions
Template <class arg1, classarg2, class result>
Struct binary_function
{
Typedef arg1 first_argument_type;
Typedef arg2 second_argument_type;
Typedef result result_type;
};
Template <class T>
Struct plus: publicbinary_function <t, t, t>
{
T operator () (const T & X, const T & Y) const {return X + Y ;}
};
2. Adapter
The adapter is similar to a converter. It is a design mode and is extended to another interface based on the original type, so that the type originally incompatible with the interface can work together.
The adapter is classified as follows: function adapter (changing the imitation function interface), container adapter (changing the container Interface), and iterator adapter (changing the iterator interface ).
2.1 container Adapter
The most obvious example is stack and queue. You can see the following code:
Template <class T, classsequence = deque <t>
Class Stack
{
Protected:
Sequence C; // deque is used for the underlying container
}
Template <class T. classsequence = deque <t>
Class queue
{
Protected:
Sequence C;
}
It can be seen from the above that both class Stack and class queue use deque as the underlying container, and then only a few functions that comply with the stack or queue principles are opened through sealing all the external interfaces of the main deque, therefore, we call stack and queue an adapter, which acts on the container.
2.2 iterator Adapter
The book describes three types of iterator adapters: insert iterator, reverseiterator, and iostream iterator. The latter two methods are more complicated in both implementation skills and understanding. Analyze it by using an instance. The Code is as follows:
Int main ()
{
1 ostream_iterator <int> outite (cout ,"");
2 int Ia [] = {0, 1, 2, 3, 4, 5 };
3 deque <int> ID (IA, Ia + 6 );
4 copy (Id. Begin (), id. End (), outite );
5 cout <Endl; // 0 1 23 4 5
6 copy (IA + 1, Ia + 2, front_inserter (ID ));
7 copy (Id. Begin (), id. End (), outite );
8 cout <Endl; // 1 0 12 3 4 5
9 deque <int >:: iterator ite = find (Id. Begin (), id. End (), 4 );
10 reverse_iterator <deque <int>: iterator> Rite (ITE)
11 cout <* ite <Endl; // 4
12 cout <* rite <Endl; // 3
}
The above 1st lines of code use iostream iteratoradapter to bind the iteration to the cout object to form an ostream_iterator with the output function. In the Code in line 6th, front_inserter (ID) is an auxiliary function to facilitate the client to use insert_iterator. After calling this function, an object iterator adapter object is actually generated:
Front_inserter_iterator <container> (X ). In the Code in line 10th, the direction of the iterator is converted through the reverse iterator adapter, but note that the iterator is reversed, although the entity location remains unchanged, that is, the position where the pointer is located remains unchanged, but the position of the object indicated by the pointer changes. One points to the right and the other points to the left. As shown in:
2.3 function Adapter
This part is the largest ethnic group among all receivers. A more abundant expression is formed by adding the imitation function. A simple example is as follows:
Not1 (bind2nd (less <int> (), 12 ))
The above expression is an expression not less than 12, where less <int> () is a function imitation, the bind2nd sub-adapter is used to convert the operations of auxiliary functions into another function.
In addition, you need to add the ptr_fun adapter of the user function pointer and the mem_fun \ men_fun_ref of the user member function pointer. Through the adapter operation, general functions and member functions can be used as imitation functions and passed to the STL algorithm.
Seven or eight series of study notes in STL source code analysis-imitation functions and adapters