STL function object)

Source: Internet
Author: User
STL function object

STL not only enables us to write complex code more easily and quickly, but also makes the written code both standard and highly optimized.

STD: vector <STD: String> names;

//...

STD: Sort (names. Begin (), names. End ());

Another elegance of STL is that it is highly configurable. In the above Code, the string element in the vector is sorted using the <) operator of the string, but in other cases, there may not always be a less than operator available, in addition, you do not want to sort data in ascending order.

Class State

{

Public:

//...

Int Population () const;

Float Avetempf () const;

//...

};

The state class is used to represent a federated state. It is neither smaller than the operator nor intended to implement one State for it, because "one State is smaller than another State" cannot tell what it means. Fortunately, STL generally allows us to specify an alternative operation similar to the less-than-like operator. This operation is called a "Comparator" because it is used to compare two values:

Inline Bool Popless (const State & A, const State & B)

{

Return A. Population () <B. Population ();

}

With a comparator for state, you can use it for sorting:

State Aunion [50];

//...

STD: Sort (aunion, aunion+ 50,Popless); // sort by population

Here we pass a pointer to the popless function as the comparator (the function name degrades to a pointer ). Because popless is passed as a function pointer, it cannot be inline in sort. If you want to get a quick sorting operation, this can only be a pity.

If you use a function object as the comparator, the situation will be much better:

Struct Popless:Public STD: binary_function <state, state, bool>

{

Bool Operator()(Const State & A, const State & B) const

{

Return A. Population () <B. Population ();

}

};

The popless type is a typical example of a properly constructed STL function object.

First, it is a function object. It reloads the function call operator, so it can be called using the syntax of common function call. This is important because STL generic algorithms such as sort are written in this way: function pointers and function objects can be used to instantiate them, as long as the two can be called using the typical function call syntax. A function object with an overloaded operator () can fully meet this syntax requirement.

Secondly, it is derived from the standard binary_function base class. This mechanism allows STL implementation of other parts to ask questions about the function object compiler. In this example, the popless type derived from binary_function allows us to find out the parameters and return value types of function objects. However, we have not used this capability here, but we can bet that someone needs it, and we hope our popless type can be used by others.

Third, this function object has no data members, no virtual functions, no declarative constructors and destructor displayed, and the implementation of operator () is inline. Function objects used as STL comparator are generally very small. Simple and fast. Of course, you can design an STL function object with a heavy implementation, but this is usually not wise. When used in collaboration with STL, another reason for avoiding (or minimizing) using data members in function objects is that STL implementation may generate several copies for a function object, it is also assumed that all these copies are consistent. To ensure that all the copies of an object are consistent, the simplest way is not to let the object carry any data members.

Now we can use this function object to sort the aunion:

STD: Sort (aunion, aunion+
50,
Popless (); // sort by population

Note the parentheses following the popless in this sort call. Popless is a type, but we must input an object of this type as a function parameter. By appending a pair of parentheses after a popless type name, a temporary popless object with no name is created, this object only exists during the function call period (this unnamed object is the "anonymous temporary object" in the total region "). You can also declare and pass in a named object:

Popless Comp;

STD: Sort (aunion, aunion+ 50,Comp); // sort by population

However, it is easier to pass in an anonymous temporary object, more suitable for habits, and fewer keys.

Another advantage of using a function object as a comparator is that a comparison operation will be processed in inline mode, while a function pointer cannot be used in inline mode. The reason is that when an sort function template is instantiated, the compiler knows that the type of the comparator is popless, so that it knows that popless: Operator () will be called, and then enables it to inline the function.

In STL, another common purpose of a function object is to use it as a scalar. Implicit is an operation that asks true/false questions about a single object (you can regard a comparator as a binary scalar ).

Struct Iswarm:Public STD: unary_function <state, bool>

{

Bool Operator()(Const State & A) const

{

Return A. avetempf ()> 60;

}

};

The STL primitive design knows that the policy is consistent with that of the STL comparator. The only exception is that the former is a mona1 function, not a binary function. Starting from the result of our previous sorted state, we can easily find a state with a warm climate and a small number of people:

State * warmandsparse=Find_if (aunion, aunion+
50,
Iswarm ());

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.