2010-11-04 256 views
1

在我的任務中,我知道我需要使用我的堆刪除功能,它返回要打印的變量。但是這項任務的要求很模糊,我很好奇,如果有人能給我一個更好的解釋。我很困惑如何使用第二個或第三個參數我知道一些排序是必需的,我需要兩個for循環,但之後,我迷路了。這裏是這個任務說的:堆二叉樹打印方法

template <class T, class P> void print_list (vector<T>&, 
    const int, const int, P); 

這個函數從堆結構中檢索項並在標準輸出中打印出來。要從堆結構中檢索單個項目,它會調用remove函數。這個函數的第一個參數是堆結構的向量,第二個參數是stdout上打印項目的分配大小,第三個參數是單行上打印項目的最大數量,最後一個參數是謂詞。

這裏是我的代碼:

#include "340.h" 

#ifndef H_PROG7 
#define H_PROG7 

// data files 

#define D1 "prog7.d1" 
#define D2 "prog7.d2" 
#define D3 "prog7.d3" 

#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 prototypes 

template<class T,class P> void insert(vector<T>&, const T&, P); 
template<class T,class P> T remove(vector<T>&, P); 

template<class T,class P> void upheap(vector<T>&, int, P); 
template<class T,class P> void downheap(vector<T>&, int, P); 

template<class T,class P> 
void get_list(vector<T>&, const char*, P); 

template<class T,class P> 
void print_list(vector<T>&, const int, const int, P); 

template<class T, class P> 
void get_list(vector<T>& v, const char* file, P func) { 
ifstream inFile("file"); 
T data; 

while(inFile >> data) { 
    inFile >> data; 
    insert(v, data, func); 
} 
} 

template<class T, class P> 
void insert(vector<T>& v, const T& data, P func) { 
v.push_back(data); 
upheap(v, v.size()-1, func); 
} 

template<class T,class P> 
void upheap(vector<T>& v, int start, P func) { 

while(start <= v.size()/2) { 

    unsigned int parent = start/2; 

    if(parent - 1 <= v.size() && v[parent - 1] > v[parent]) 
    parent = parent - 1; 

    if(v[start] <= v[parent]) 
    break; 

    swap(v[start], v[parent]); 
    start = parent; 
} 
} 

template<class T,class P> 
void downheap(vector<T>& v, int start, P func) { 

while(start <= v.size()/2) { 

    unsigned int child = 2 * start; 

    if(child + 1 <= v.size() && v[child + 1] > v[child]) 
    child = child + 1; 

    if(v[start] >= v[child]) 
    break; 

    swap(v[start], v[child]); 
    start = child; 
} 
} 

template<class T,class P> 
T remove(vector<T>& v, P func) { 
swap(v[0], v.back()); 
T& item = v.back(); 

v.pop_back(); 
downheap(v, 1, func); 

return item; 
} 

template<class T,class P> 
void print_list(vector<T>& v, const int size, const int line, P func) { 

for(int i = 1; i < v.size(); i++) { 
    cout << remove(v, func) << " "; 
} 
} 

#endif 
+0

對不起,我不明白。需要更多細節。順便說一下,'stdout'是用於C - 在C++中,你使用'cout'。 – 2010-11-04 00:47:37

+0

@steve Townsend只是添加了我的代碼,希望能夠澄清一些事情。 – rajh2504 2010-11-04 01:19:25

回答

0

從我可以收集它看起來像第二個參數是空間列表中的一個元素時,允許它打印出來佔用數量。第三個參數是允許在一行上打印的元素的數量。在你的print_list函數中,你需要跟蹤你已經打印了多少個元素,根據這個參數確定你是否需要去下一行。使用這兩個,你應該能夠很好地對齊你的輸出。您需要了解如何執行輸出格式。

例如,列表= [1,9,14,2000年,244,777,圖3,98102,88,53,14],大小= 6,線= 3 輸出:

 1  9 14 
    2000 244 777 
    3 98102 88 
    53 14 

你的最後一個參數是謂詞,你似乎實際上並沒有在你的任何函數中使用這個函數(當你根本沒有使用參數的時候,你會做出錯誤的表示)。謂詞函數參數應該用於執行傳入類型T所特有的內容。例如,在print_list中,它應該用於打印T類型的變量。如果按照當前的方式打印元素,不能保證該類型將被正確打印。當然,它可以用於像int和double這樣的基本類型,但想象一下包含幾個不同成員的類型。想一想如何在其他功能中使用這個謂詞 - 你能比較一下「<」還是「>」?