how to iterate through a fibonacci heap (boost) that contains deque<MyData> element

889 views Asked by At

I'm using Fibonacci heap(boost) to order array of elements but I'm not able to iterate through the heap. The code is like this:

#include <deque>
#include <boost/heap/fibonacci_heap.hpp>
#include <iostream>

struct MyData {
     ...
     long int id;

     ...
}; 

struct MyCompare{
    bool operator()(const deque<MyData> &a, const deque<MyData> &b) const
    {
        return a[a.size()-1].id > b[b.size()-1].id;
    }
};


int main()
{   deque<MyData> array1;
    deque<MyData> array2;

    MyData app;
    app.id=15;
    array1.push_front(app);
    app.id=10;
    array1.push_front(app);

    app.id=5;
    array2.push_front(app);
    app.id=2;
    array2.push_front(app);

    boost::heap::fibonacci_heap<deque<MyData>, boost::heap::compare<MyCompare> > fib_heap;  
    fib_heap.push(array1);
    fib_heap.push(array2);
    boost::heap::fibonacci_heap<deque<MyData>, boost::heap::compare<MyCompare> >::iterator it;
    boost::heap::fibonacci_heap<deque<MyData>, boost::heap::compare<MyCompare> >::iterator beg =fib_heap.begin();
    boost::heap::fibonacci_heap<deque<MyData>, boost::heap::compare<MyCompare> >::iterator end =fib_heap.end();

    for (it=beg;it!=end; ++it) {
        deque<MyData> elem;
        elem =*it;
        for (int k=0;k < elem.size();k++)
            cout<<" "<<elem[k].id;
        cout<<"\n";         
    }


}

It give me this error at the line "elem = *it": Error in instantion of recursive_tree_iterator: recursive_tree_iterator(void): adaptor_type(0) {}

Is there any way to do that? Or using another ordered heap instead of Fibonacci? Thank you very much.

1

There are 1 answers

0
J. Calleja On

I think the error is that you cannot default-construct the iterator. Try eliminating the creation of the it variable and moving it inside the for loop:

boost::heap::fibonacci_heap<deque<MyData>, boost::heap::compare<MyCompare> >::iterator beg =fib_heap.begin();
boost::heap::fibonacci_heap<deque<MyData>, boost::heap::compare<MyCompare> >::iterator end =fib_heap.end();

for (boost::heap::fibonacci_heap<deque<MyData>, boost::heap::compare<MyCompare> >::iterator it=beg;it!=end; ++it) {
    deque<MyData> elem;
    elem =*it;
    for (size_t k=0;k < elem.size();k++)
        cout<<" "<<elem[k].id;
    cout<<"\n";         
}

In order to iterate in heap order, you can use:

ordered_iterator ordered_begin(void) const;
ordered_iterator ordered_end(void) const;

Also, I think the code would benefit of using the auto keyword:

for (auto it=fib_heap.begin(), end=fib_heap.end();it!=end; ++it) {
    deque<MyData> elem;
    elem =*it;
    for (size_t k=0;k < elem.size();k++)
        cout<<" "<<elem[k].id;
    cout<<"\n";         
}