C++ C++;堆实现

C++ C++;堆实现,c++,stl,heap,C++,Stl,Heap,好的,所以我试图用向量实现一个堆结构,但是我不能让它正常工作。第一个堆工作正常,但由于某种原因,stl sort\ U heap函数无法正常工作。我似乎无法让我的堆按降序打印。这是我的标题: // data files #define D1 "***************************" #define INT_SZ 4 // width of integer #define FLT_SZ 7 // width of floating-pt number #defin

好的,所以我试图用向量实现一个堆结构,但是我不能让它正常工作。第一个堆工作正常,但由于某种原因,stl sort\ U heap函数无法正常工作。我似乎无法让我的堆按降序打印。这是我的标题:

// data files

#define D1 "***************************"

#define INT_SZ 4    // width of integer
#define FLT_SZ 7    // width of floating-pt number
#define STR_SZ 12   // width of string

#define INT_LN 15   // no of integers on single line
#define FLT_LN 9    // no of floating-pt nums on single line
#define STR_LN 5    // no of strings on single line

// function and class prototypes

// stores items from input file into vector
template < class T >
void get_list ( vector < T >&, const char* );

// construct heap from items in vector
template < class T, class P >
void construct_heap ( vector < T >&, P );

// class to compare absolute values
template <class T> class abs_less {
public:
    bool operator ( ) ( const T&, const T& ) const;
};

// structure to print items in heap, where T is data type of items,
// W is allocated size in printout, and L is max num of items printed
// on single line

template < class T, const int W, const int L >
struct print_list {
    int sz, cnt; // size of heap and counter for printing
    print_list ( const int&, const int& = 0 ); // constructor
    void operator ( ) ( const T& );
};
这是我的输出。为什么我的堆不按降序打印

first heap - ascending order:

-942 -925 -912 -912 -880 -831 -804 -761 -749 -746 -695 -677 -655 -647 -598
-573 -546 -539 -532 -525 -513 -509 -495 -444 -403 -390 -382 -369 -361 -361
-313 -305 -288 -282 -263 -235 -220 -220 -202 -197 -197 -187 -181 -167 -145
-136 -115  -72  -45   -9   -7   28   40   45   69   71  139  144  164  179
 213  251  293  302  348  349  352  393  404  465  475  514  516  517  531
 534  547  557  560  560  571  618  646  654  677  733  739  746  774  830
 834  863  875  895  912  913  916  934  958  966

first heap - descending order:

 958  916  746  913  895  875  739  534  863  834  618  830  774  733  677
 531  293  393  654  646  352  571  560  516 -361  514  560  557  547  517
 139   69  349  475  164 -220   45   -9  465 -167  -72  404  302 -202  348
 251   40   28 -115 -305 -942 -444 -197 -513 -880 -912  179  144   71   -7
 -45 -136 -761 -145 -495 -181 -525 -187 -197 -546 -220 -282 -235 -912 -677
-288 -598 -804 -313 -361 -509 -369 -539 -403 -390 -749 -925 -746 -695 -573
-831 -382 -532 -647 -263  213  912  934 -655  966
您的
constructHeap()
函数似乎对
std::make_heap()
std::sort_heap()
使用了两个不同的谓词:我认为应该是这样的

std::make_heap(v.begin(), v.end(), pred);
std::sort_heap(v.begin(), v.end(), pred);
std::sort_heap()
范围内容的先决条件是,根据谓词,该范围是一个堆

顺便说一句,在
abs\u less
函数中,您应该只返回结果:

template<class T>
bool abs_less<T>::operator()(const T& x, const T& y) const {
    return abs(x) > abs(y);
}
你可能应该使用

v.clear();

除了更具可读性之外,这些版本可能更高效

template<class T>
bool abs_less<T>::operator()(const T& x, const T& y) const {
    return abs(x) > abs(y);
}
while(!v.empty())
    v.pop_back();
v.clear();
v.erase(v.begin(), v.end());