0
我想創建一個屬於類A成員的「通用」priority_queue,這樣我就不需要在編譯時指定比較器函子類。我將在運行時選擇比較器通道。我怎樣才能達到這個目標?下面是我的用例的一個簡單例子。如何在運行時指定priority_queue的比較器類
我無法使用任何C++ 11功能。
class A{
private:
priority_queue<T, vector<T>, ?> *pq;
public:
A(string);
~A();
};
A::A(string s) {
if(s == "1")
pq = new priority_queue<T, vector<T>, Mycomparator1>;
else (s == "2")
pq = new priority_queue<T, vector<T>, Mycomparator2>;
}
A::~A(){
delete pq;
}
struct Mycomparator1 {
bool operator()(const T&a, const T&b){
return a.x > b.x;
}
};
struct Mycomparator2 {
bool operator()(const T&a, const T&b){
return a.y > b.y
}
};
int main(){
string s(argv[1]);
A(s);
}
如何將std :: function作爲比較器,因爲它已經實現了類型擦除,而這正是您可能需要的。編輯:抱歉,沒有注意到'我不能使用C++ 11'。 Geez 2017年已經... –