Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the mem_fn class template in the standard library of Clipper 11

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

The main content of this article is to explain how to use the mem_fn class template of Clipper 11 standard library. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use the mem_fn class template of the Clear11 standard library.

Customized algorithm

Algorithm is a very important part of C++ standard library. C++ separates the data structure from the algorithm by the way of algorithm + container. The advantage of this is to give users maximum flexibility.

For example, the following Tracer class.

We can easily define the data structure that holds the Tracer object:

Then it is very convenient to sort by using the sorting algorithm:

The compare in the code is another comparison function defined:

Use this function to select the object to compare and determine the result of the comparison.

Strictly speaking, what is required in the algorithm is not a function, but a callable object. As you can see from the previous articles in this series, callable objects are functions that can be performed in the form of fun (a). The callable objects in C++ include functions, function objects, lambada expressions, parameter bindings and so on, all of which can be used as parameters of the algorithm.

Using function to generate callable objects

The method mentioned above is very easy to understand, but it is a little troublesome, and we want to use the member function compare member function of Tracer directly. This requirement can be addressed using the function introduced in Clover 11:

Function is a template that specifies the form of callable objects. The form of tcomp is specified in the example: the return value is bool and the parameter is two Tracer&. After assigning the member function compare of Tracer to tcomp in the code, you can use it:

In this case, the tcomp (T1 ~ T2) action can be understood as t1.compare (T2). In this way, we can convert the member functions of the class into callable objects. So the sorting code can be written as follows.

Using mem_fn to generate callable objects

Using the function method is still a bit of a hassle: although the signature of & Tracer::compare has determined the form of the callable object, the programmer still needs to specify something else. The solution to this problem is to use mem_fn (not mem_fun) to generate callable objects:

Mem_fn infers the type of callable function from the member function pointer, omitting the additional steps specified.

At this point, I believe that you have a deeper understanding of how to use the mem_fn class template in the standard library 11, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report