为泛型类传递比较功能 [英] Passing compare function for a generic class

查看:82
本文介绍了为泛型类传递比较功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个我正在使用堆(使用数组)的优先级队列。优先级队列将是通用的,因此只要客户端通过构造函数传递比较函数以比较这两种类型,就可以接受所有数据类型。

I want to create a priority queue for which I am using a heap(using array).The priority queue will be generic thus accept all data types as long as the client pass a compare function through constructor to compare the two types.

如何创建将比较函数作为参数的构造函数?此外,如何在我检查时调用比较函数

How can I create a constructor that will accept the compare function as a parameter? Moreover how can I make the compare function to be called when I check

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进行比较。

The PQueue class is implemented as an array. As the adding,bubbling-up, heapifying needs to compare two ValType I want them to compare using compareNode.

推荐答案

让我先

您可以通过将参数的类型声明为指针函数的类型来简单地将函数作为参数传递。您还可以使用指向函数的指针类型的变量。例如,如果函数的声明为

You can simply pass a function as parameter by declaring the type of that parameter to be the type of pointer function. You can also have variables of type pointer to function. For instance, if the declaration of your function is

int compareNode(node a, node b)

然后您可以执行以下操作:

then you could do something like this:

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

http://ideone.com/EPjbya

希望您可以使用它。

现在是更通用的示例。

C ++ 11引入了lambda。 http://www.cprogramming.com/c++ 11 / c ++ 11-lambda-closures.html http:// www.stroustrup.com/C++11FAQ.html#lambda

C++11 introduces 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标准编译此代码。

http://ideone.com/ryQmAn You need to compile this code for C++11 standard.

此处是模板Comparator可以同时是指向函数和lambda的指针。如果您对lambda感兴趣,我上面提供的两个链接应该可以帮助您入门。

Here the template Comparator can be both pointer to function and lambda. If you are interested in lambdas, the two links I provided above should get you started.

这篇关于为泛型类传递比较功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆