Lambda and priority_queue as well as function and bind

Source: Internet
Author: User

A lambda is a callable object, which is an object and each lambda has its own different type.

When I was young I thought it would be strange to mix STL with Lambda, like I couldn't define a priority queue like this:

priority_queue<int, vector<int;, [] (intint b) {return a > B;} > que;

But you can use sort like this.

Sort (Vec.begin (), Vec.end (), [] (intint b) {return a < b;});

And you can use sort like this.

bool Less (intintreturn a <bool (*CMP) (intint = Less;sort ( vec.begin (), Vec.end (), mycmp); sort (Vec.begin (), Vec.end (), less);   

The question arises because the relationship between the function object (also called the callable object) and the type parameter of the template is not known, and first explains how to properly instantiate the priority_queue using a Lambda object:

#include <iostream>#include<queue>#include<vector>#include<utility>usingmy_pair_t = std::p air<size_t,BOOL>; usingmy_container_t = std::vector<my_pair_t>; intMain () {Auto My_comp=        [](Constmy_pair_t& E1,Constmy_pair_t&E2) { returnE1.first >E2.first;}; std::p riority_queue<my_pair_t, my_container_t, Decltype (My_comp)>Queue (My_comp); Queue.push (Std::make_pair (5,true)); Queue.push (Std::make_pair (3,false)); Queue.push (Std::make_pair (7,true)); Std::cout<<Std::boolalpha;  while(!Queue.empty ()) {        Constauto& p =Queue.top (); Std::cout<< P.first <<" "<< P.second <<"\ n";    Queue.pop (); }}

1, first here My_comp is an object, because each lambda object has an anonymous type, so you can only use Auto to express the type of My_comp

2, the Declaration of the priority queue is this:

template<    class  T,    class Container = Std::vector<t>,      Classclass priority_queue;

So to instantiate a priority queue, the angle brackets have to fill TypeName ah, because the type of lambda object is anonymous, so with Decltype, and then the light is not possible, this will look at the constructor:

Explicit Const compare& Compare = Compare (),                         const container& cont = Container ());

As you can see, if we construct without specifying a specific compare object, we construct one with the default constructor of the TypeName compare, but the anonymous type of the lambda expression is not a default constructor.

So if you want to initialize the priority queue correctly, you have to pass the lambda expression itself in the constructor.

3, function pointers, functions, lambda expressions, relationships between function objects

First look at the one overload declaration of sort:

Template <class compare>  void sort (randomaccessiterator first, Randomaccessiterator last, Compare comp);

Here the sort accepts a function object, so it is possible to pass the Lambda object directly, because the lambda object belongs to the function object, and in C + + It is the function object that can use the function call operator, so

function objects are: functions, function pointers, objects that overload the () class, and lambda objects.

Note that functions and function pointers are two different things, as though arrays are passed as pointers as arguments, but they are still not the same thing, namely:

While non-referencing, arrays and functions are converted to pointers (including template argument inference), but they are still not pointers, examples of pointers do not have to be said, and examples of functions are given:

Template <typename t>void t (const t& t) {    << std::is_pointer<t>: : Value << Endl;     <typename t>void  E (t t) {    << std::is_pointer<t>::value << Endl;    T ();} void f () {int  main () {    T (f);     << Endl;    E (f);     return 0 ;}

The first call output is 0, the second is 1, which means that the function itself and the function pointer are also two things

This explains how to use sort and priority_queue at the beginning.

Lambda and priority_queue as well as function and bind

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.