먼저 간단한 대답을 한 다음 더 다양한 대답을 알려주십시오.
매개 변수의 유형을 포인터 함수의 유형으로 선언하여 함수를 매개 변수로 간단히 전달할 수 있습니다. 함수에 대한 포인터 유형의 변수를 가질 수도 있습니다. 예를 들어, 경우 함수의 선언은
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;
}
http://ideone.com/EPjbya
희망이 당신이 사용할 수 있습니다.
이제 더 다양한 예를 살펴보십시오.
C++ 11은 람다를 도입합니다. http://www.cprogramming.com/c++11/c++11-lambda-closures.htmlhttp://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;
}
당신은 C++ 11 표준이 코드를 컴파일 할 필요가있다.
여기서 템플릿 비교기는 함수와 람다에 대한 포인터가 될 수 있습니다. 람다에 관심이 있다면 위에 제공된 두 개의 링크를 시작해야합니다.
Java 사고 방식을 제거하면 C++로 전환 할 때 유용합니다 (그 반대의 경우도 마찬가지 임). 특히 Java generics보다 훨씬 뛰어난 C++ 템플릿에 대해서는이 사실이 중요합니다. C++에서 템플리트는 유형에 정의 된 연산자를 참조 할 수 있으므로'a dasblinkenlight
그러나 <연산자가 두 유형을 비교하는 방법은 무엇입니까? 나는 고객이 나를 비교하기를 원하는 방식을 전달하기를 원합니다. 유형이 <다른 유형보다 더 큰지를 결정하면서 <비교 연산자를 실행하고 싶습니다. – user2511713
원하십니까? 두 가지 유형을 비교하거나 두 가지 유형의 두 가지 인스턴스를 비교 하시겠습니까?여기서 용어가 중요합니다. – juanchopanza