增加和减少优先级队列的模板函数
Template function for increasing and decreasing priority queue
我想编写一个模板函数来打印递增和递减优先级队列。目前我已经将其实现为
void print_queue1(priority_queue<int> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
// min-heap
void print_queue2(priority_queue<int, vector<int>, greater<int>> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
有什么方法可以编写一个可以做到这一点的模板函数吗?
您可以为此使用 variadic function template。由于无论队列类型如何,逻辑都是相同的,我们可以接受任何类型的队列,例如
template <typename... Params>
void print_queue(priority_queue<Params...> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
这里,Params
会由编译器从提供给你的priority_queue
的模板参数中推导出来,并为每个不同的参数集戳出一个具体的函数。
模板 class std::priority_queue
使用 three template type parameters.
template<
class T,
class Container = std::vector<T>,
class Compare = std::less<typename Container::value_type>
> class priority_queue;
您可以在函数中使用相同的三个参数来接受 std::priority_queue
的任何实例化。
template<class T, class Container, class Compare>
void print_queue(priority_queue<T,Container,Compare> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
或者,您可以remove/restrict其中任何一个来强制执行优先级队列的子集..
template<class Container, class Compare>
void print_queue(priority_queue<int,Container,Compare> q, string s){
// This function is really only designed for priority queues of int!
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
我想编写一个模板函数来打印递增和递减优先级队列。目前我已经将其实现为
void print_queue1(priority_queue<int> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
// min-heap
void print_queue2(priority_queue<int, vector<int>, greater<int>> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
有什么方法可以编写一个可以做到这一点的模板函数吗?
您可以为此使用 variadic function template。由于无论队列类型如何,逻辑都是相同的,我们可以接受任何类型的队列,例如
template <typename... Params>
void print_queue(priority_queue<Params...> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
这里,Params
会由编译器从提供给你的priority_queue
的模板参数中推导出来,并为每个不同的参数集戳出一个具体的函数。
模板 class std::priority_queue
使用 three template type parameters.
template<
class T,
class Container = std::vector<T>,
class Compare = std::less<typename Container::value_type>
> class priority_queue;
您可以在函数中使用相同的三个参数来接受 std::priority_queue
的任何实例化。
template<class T, class Container, class Compare>
void print_queue(priority_queue<T,Container,Compare> q, string s){
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}
或者,您可以remove/restrict其中任何一个来强制执行优先级队列的子集..
template<class Container, class Compare>
void print_queue(priority_queue<int,Container,Compare> q, string s){
// This function is really only designed for priority queues of int!
cout << "Value of prior queue " << s << " is : [";
while(!q.empty()){
cout << " " << q.top() << ",";
q.pop();
}
cout << "]" << endl;
}