Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::make_heap with pairs

Is it possible to do make_heap() with a pair in a vector?

I'm using:

 std::vector< std::pair < int , tablero& > > lista_abierta_;

I use the object function to order the pair by the first member, but it crashes.

The code is:

#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#include "8_puzzle.h"
#include "tablero.h"

using namespace std;

class comp {
public:
    bool operator()(pair < int, tablero&> a, pair < int, tablero&> b) const {
        return a.first > b.first;
    }
};

pair < int, tablero& > puzzle::A_estrella::tope()
{
    pair < int, tablero& > l=lista_abierta_.front();

    pop_heap(lista_abierta_.begin(),lista_abierta_.end());
    lista_abierta_.pop_back();

    return l;
}

[Taken from here]

like image 772
nsb Avatar asked Jan 28 '26 13:01

nsb


1 Answers

As long as std::pair<T, U> provides operator< (meaning : T and U provide operator<), I don't see any issue in using make_heap.

like image 160
icecrime Avatar answered Jan 31 '26 04:01

icecrime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!