我想创建一个优先级队列,我正在使用堆(使用数组)。优先级队列将是通用的,因此只要客户端通过构造函数传递比较函数来比较这两种类型,就接受所有数据类型。
如何创建一个将比较函数作为参数接受的构造函数?此外,当我检查
时,如何调用compare函数return (Type a==Type b)
例如
struct node{
string val1;
string val2;
vector<node *> connectedNodes;
};
int compareNode(node a,node b){
//describe the compare
}
int main(){
PQueue<node> q(compareNode);
}
PQueue类实现为数组。随着添加,冒泡,堆积需要比较两个ValType我希望它们使用compareNode进行比较。
答案 0 :(得分:0)
您不必这样做:不使用数组,在c ++中使用STL库的内置优先级队列。它有自己的比较功能,你可以改变。
参考:http://www.cplusplus.com/reference/queue/priority_queue/
您还可以查看topcoder教程(用于算法用法)。
答案 1 :(得分:0)
首先让我给你一个简单的答案,然后是一个更通用的答案。
您可以通过将该参数的类型声明为指针函数的类型来简单地传递函数作为参数。您还可以使用指向函数的类型指针的变量。例如,如果您的函数声明是
int compareNode(node a, node b)
那么你可以这样做:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct node{
string val1;
string val2;
vector<node *> connectedNodes;
};
int compareNode(node a,node b){
//describe the compare
return a.val2.compare(b.val2); // or any other code
}
template <class T>
class PQueue {
protected:
// this declares a protected member named compareFunction of type pointer to a function which takes 2 T parameters and returns a int. Note that all the parenthesis are mandatory
int (*compareFunction)(T, T);
public:
PQueue (int (*compareFunctionParameter)(T, T)) : compareFunction(compareFunctionParameter) {
// this constructor receives a pointer to function and initializes it's member to that pointer. If the constructor initialization list confuses you, you can read 'compareFunction = compareFunctionParameter '
}
int someMethod() {
// call the function through the pointer you have:
node n1, n2;
n1.val1 = "node1_val1";
n1.val2 = "zzz";
n2.val1 = "node2_val1";
n2.val2 = "aaa";
return compareFunction(n1, n2);
}
};
int main() {
PQueue<node> pq(compareNode);
cout << pq.someMethod() << endl;
return 0;
}
希望你可以使用它。
现在来看一个更通用的例子。
C ++ 11引入了lambdas。 http://www.cprogramming.com/c++11/c++11-lambda-closures.html http://www.stroustrup.com/C++11FAQ.html#lambda
#include <iostream>
#include <vector>
#include <string>
#include <functional>
using namespace std;
struct node{
string val1;
string val2;
vector<node *> connectedNodes;
};
int compareNode(node a,node b){
//describe the compare
return a.val2.compare(b.val2); // or any other code
}
template <class T, class Comparator>
class PQueue {
protected:
Comparator compareFunction;
public:
PQueue (Comparator compareFunctionParameter) : compareFunction(compareFunctionParameter) {
}
int someMethod() {
// call the function
node n1, n2;
n1.val1 = "node1_val1";
n1.val2 = "zzz";
n2.val1 = "node2_val1";
n2.val2 = "aaa";
return compareFunction(n1, n2);
}
};
int main() {
// queue with pointer to function
PQueue<node, int (*)(node, node)> pq(compareNode);
cout << pq.someMethod() << endl;
// queue with lamda (anonimous function)
PQueue<node, std::function<int (node, node)>> pq_lambda([](node a, node b) -> int {return a.val1.compare(b.val1);} );
cout << pq_lambda.someMethod() << endl;
return 0;
}
http://ideone.com/ryQmAn您需要为C ++ 11标准编译此代码。
这里模板Comparator既可以是函数指针,也可以是lambda。如果您对lambdas感兴趣,我上面提供的两个链接应该可以帮助您开始。