C++中元组的优先级队列与示例
优先队列
优先队列 是一种容器适配器,专门设计成队列的第一个元素是队列中所有元素中最大的,并且元素的顺序是非递增的(因此我们可以看到队列的每个元素都有一个优先级{固定顺序} )。
与优先队列相关的函数:
- empty():该函数返回队列是否为空。
- size():此函数返回队列的大小。
- top():返回对队列最顶层元素的引用。
- push(x):此函数在队列末尾添加元素“ x ”。
元组
元组是一个可以容纳多个元素的对象。元素可以是不同的数据类型。元组的元素按照访问顺序初始化为参数。
与元组关联的函数:
- get(): get() 用于访问元组值并修改它们,它接受索引和元组名称作为参数来访问特定的元组元素。
- make_tuple(): make_tuple() 用于给元组赋值。传递的值应该与元组中声明的值顺序一致。
本文重点介绍如何在 C++ 中使用元组的优先级队列。在设计复杂的数据结构时,元组的优先级队列非常有用。
Syntax 1: Max-heap priority queue of tuples:
priority_queue> priorityQueue;
Syntax 2: Min-heap priority queue of tuples:
priority_queue,
vector>,
greater>> priorityQueue;
Syntax 3: Customized priority queue of tuples (using comparator):
priority_queue,
vector>, comparator> priorityQueue;
元组的最大堆优先级队列:
默认情况下,优先级队列是最大堆。因此,对于优先级队列中的一对元组,比较它们的第一个元素,如果两个元组的第一个元素相等,则比较它们的第二个元素,如果第二个元素也相等,则第三个元素是比较的。具有较大元素的元组成为优先级队列的最顶部元素。
示例 1:下面是实现元组最大堆优先级队列的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue > priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue of
// tuple
priority_queue > priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(1, 1, 'G');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(2, 2, 'e');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(3, 3, 'e');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(4, 4, 'k');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue > priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue
// of tuple
priority_queue > priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0, "Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 0,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a min-heap priority
// queue of tuple
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(1, 1, 'G');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(2, 2, 'e');
// pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(3, 3, 'e');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(4, 4, 'k');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue of
// tuple
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0, "Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 0,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Comparator
// we can use class also but then
// we will need to make the function
// public as class is by default
// private
struct myComparator
{
int operator()(const tuple& tuple1,
const tuple& tuple2)
{
// Second element of tuples is equal
if (get<1>(tuple1) == get<1>(tuple2))
{
if (get<0>(tuple1) == get<0>(tuple2))
{
if (get<2>(tuple1) >= get<2>(tuple2))
return true;
return false;
}
return get<0>(tuple1) >= get<0>(tuple2);
}
return get<1>(tuple1) >= get<1>(tuple2);
}
};
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
myComparator>
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue
// of tuple
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 1,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Comparator
// we can use class also but then
// we will need to make the function
// public as class is by default private
struct myComparator
{
int operator()(const tuple& tuple1,
const tuple& tuple2)
{
// Second element of tuples is equal
if (get<1>(tuple1) == get<1>(tuple2))
{
// first element of tuples is equal
if (get<0>(tuple1) == get<0>(tuple2))
{
if (get<2>(tuple1) <= get<2>(tuple2))
return true;
return false;
}
return get<0>(tuple1) <= get<0>(tuple2);
}
return get<1>(tuple1) <= get<1>(tuple2);
}
};
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue of
// tuples by passing a custom comparator
priority_queue,
vector >,
myComparator>
priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 1,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
[ 4 4 k]
[ 3 3 e]
[ 2 2 e]
[ 1 1 G]
示例 2:下面是演示元组最大堆优先级队列工作的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue > priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue
// of tuple
priority_queue > priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0, "Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 0,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
[ 1 3 Programming]
[ 1 2 Geeks]
[ 0 0 Programming]
[ 0 0 Geeks]
元组的最小堆优先级队列:
对于最小堆优先级队列中的一对元组,比较它们的第一个元素,如果两个元组的第一个元素相等,则比较它们的第二个元素,如果第二个元素也相等,则比较第三个元素进行比较。具有较小元素的元组成为优先级队列的最顶部元素。
示例 1:下面是实现元组最小堆优先级队列工作的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a min-heap priority
// queue of tuple
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(1, 1, 'G');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(2, 2, 'e');
// pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(3, 3, 'e');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(4, 4, 'k');
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
[ 1 1 G]
[ 2 2 e]
[ 3 3 e]
[ 4 4 k]
示例 2:下面是实现元组最小堆优先级队列工作的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue of
// tuple
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0, "Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 0,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
[ 0 0 Geeks]
[ 0 0 Programming]
[ 1 2 Geeks]
[ 1 3 Programming]
自定义的元组优先级队列:
如何使用 STL 实现最小堆? 是必须的先决条件,在这里我们将学习如何通过将比较器作为参数传递给优先级队列来自定义元组的优先级队列。
示例 1:下面是实现自定义元组优先级队列的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Comparator
// we can use class also but then
// we will need to make the function
// public as class is by default
// private
struct myComparator
{
int operator()(const tuple& tuple1,
const tuple& tuple2)
{
// Second element of tuples is equal
if (get<1>(tuple1) == get<1>(tuple2))
{
if (get<0>(tuple1) == get<0>(tuple2))
{
if (get<2>(tuple1) >= get<2>(tuple2))
return true;
return false;
}
return get<0>(tuple1) >= get<0>(tuple2);
}
return get<1>(tuple1) >= get<1>(tuple2);
}
};
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
myComparator>
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue
// of tuple
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 1,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
[ 0 0 Geeks]
[ 0 1 Programming]
[ 1 2 Geeks]
[ 1 3 Programming]
在上述程序中,对于一对元组,首先比较元组的第二个元素,如果该值相等,则比较第一个元素,如果两个元组的第一个元素相等,则比较第三个元素。否则,具有较小元素的元组将成为最顶部的元素。
示例 2:下面是演示自定义元组优先级队列工作的 C++ 程序。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Comparator
// we can use class also but then
// we will need to make the function
// public as class is by default private
struct myComparator
{
int operator()(const tuple& tuple1,
const tuple& tuple2)
{
// Second element of tuples is equal
if (get<1>(tuple1) == get<1>(tuple2))
{
// first element of tuples is equal
if (get<0>(tuple1) == get<0>(tuple2))
{
if (get<2>(tuple1) <= get<2>(tuple2))
return true;
return false;
}
return get<0>(tuple1) <= get<0>(tuple2);
}
return get<1>(tuple1) <= get<1>(tuple2);
}
};
// Function to print priority queue
// contents. Deliberately passing it
// call by value since we don't want
// to remove elements from the priority
// queue
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a tuple itself
tuple Tuple =
priorityQueue.top();
cout << "[ ";
cout << get<0>(Tuple) << ' ' <<
get<1>(Tuple) << ' ' <<
get<2>(Tuple);
cout << ']';
cout << '\n';
// Pop out the topmost tuple
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Declaring a priority queue of
// tuples by passing a custom comparator
priority_queue,
vector >,
myComparator>
priorityQueue;
// Declaring a tuple
tuple tuple1;
// Initializing the tuple
tuple1 = make_tuple(0, 0,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple1);
// Declaring a tuple
tuple tuple2;
// Initializing the tuple
tuple2 = make_tuple(0, 1,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple2);
// Declaring a tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(1, 2,
"Geeks");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple3);
// Declaring a tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(1, 3,
"Programming");
// Pushing the tuple in the
// priority queue
priorityQueue.push(tuple4);
// Calling print function
print(priorityQueue);
return 0;
}
[ 1 3 Programming]
[ 1 2 Geeks]
[ 0 1 Programming]
[ 0 0 Geeks]
在上面的程序中,对于一对元组,首先比较元组的第二个元素,如果这个值相等,则比较第一个元素,如果两个元组的第一个元素相等,则比较第三个元素。否则,具有较大元素的元组将成为最顶部的元素。