C ++ Primer generic algorithm reading

xiaoxiao2021-03-06  89

This month, the VC interface is somewhat refined, and the experience value has also improved, but for GDI operations, there is not much contact, and the time to take a dozen pages of C Primer, the duplication of template, the generic algorithm is also 胧: mainly no Actually applied, but can't be said to be white, but it is a bit helpful for the extension of thinking.

For example, the efficiency comparison between the function pointer and the Inline function in the generic algorithm is still good for the INLINE function.

Active example - implement string sorting, use the Stable_Sort algorithm here, while requesting by the length of the string, the following uses the Less_than function to implement two strings less than, equal to operation:

Bool Less_Than (Const String & S1, Const String & S2)

{

Return S1.SIZE ()

}

Then passes the function pointer Less_than as a parameter to Stable_Sort, and the sort operation is implemented. Passing the function pointer also abandoned inline, knowing that the function using the inline will be expanded when compiling, the code in the inline function is directly "embedded" after compiling, and the program is directly executed directly to the LESS_THAN function. The middle code, not when the general function is called, first look for the function address, and then calls the code. How to implement the INLINE function? The simplest is to do a function object (Stable_sort can accept function pointer and object parameters), note that the operator is overloaded, and the comparison function is reserved, and the Inline feature is retained:

// Function object: less than an instance of the operation is implemented as operator ()

Class lessthan {

PUBLIC:

??? Bool operator () {const string & s1, const string & s2) {return s1.size ()

}

?

转载请注明原文地址:https://www.9cbs.com/read-122544.html

New Post(0)