C++ priority_queue with lambda comparator error

前端 未结 4 1036
时光说笑
时光说笑 2020-11-30 21:59

I have the following erroneous code which I am trying to compile in VC2010, but I\'m getting the error C2974 this only occurs when I include the lambda expression, so I\'m g

4条回答
  •  甜味超标
    2020-11-30 22:31

    priority_queue takes the comparator as a template argument. Lambda functions are objects, and thus can't be used as template arguments (only very few types can be, among them integral types).

    You can try using decltype there:

    priority_queue< adjlist_edge , vector,
                   decltype( [](adjlist_edge a, adjlist_edge b) -> bool {
                    if(a.second > b.second){ return true; } else { return false; }
                   })>
    adjlist_pq( [](adjlist_edge a, adjlist_edge b) -> bool {
                    if(a.second > b.second){ return true; } else { return false; }
                 } );
    

    Failing that (and it will), you can use function<>:

    priority_queue< adjlist_edge , vector,
                    function >
    adjlist_pq( [](adjlist_edge a, adjlist_edge b) -> bool {
                    if(a.second > b.second){ return true; } else { return false; }
                } );
    

提交回复
热议问题