2010-11-09 67 views
1

是否有可能在一個向量中使用一對make_heap()std :: make_heap with pairs

我使用:

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

我使用對象函數訂購一對由所述第一構件,但它會崩潰。

的代碼是:

#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; 
} 

+1

嗨NSB,歡迎堆棧溢出。請根據[格式指南](http://stackoverflow.com/editing-help)嘗試格式化您的文章。謝謝。 – jpjacobs 2012-01-06 12:25:41

+1

你不能在標準容器內有一個'_reference_對,你可以嗎? – 2012-12-29 03:59:08

回答

1

只要std::pair<T, U>提供operator<(意爲:TU提供operator<)從here採取],我看不出有什麼問題,在使用make_heap 。

0

只要T提供bool operator<(const T &, const T &)或者您明確地傳遞比較器,您可以調用std::make_heap<T>

你必須從

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

線23更改爲

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