C++ 中映射的优先级队列及示例
优先队列
优先级队列是一种容器适配器,专门设计为队列的第一个元素是队列中所有元素中最大的元素,并且元素按非递增顺序排列(因此我们可以看到队列中的每个元素都有一个优先级{固定顺序})。通常,优先级队列有两种类型:最大堆和最小堆,但我们总是可以将比较器传递给优先级队列。
与优先队列一起使用的函数:
- empty():该函数返回队列是否为空。
- size():此函数返回队列的大小。
- top():返回对队列最顶层元素的引用。
- push(x):此函数在队列末尾添加元素“x”。
地图
地图是以映射方式存储元素的关联容器。每个元素都有一个键值和一个映射值。没有两个映射值可以具有相同的键值。在内部,它被实现为自平衡 BST。
与地图一起使用的功能:
- begin():返回映射中第一个元素的迭代器
- end():返回一个迭代器,指向映射中最后一个元素之后的理论元素
- size():返回地图中的元素个数
- empty():返回地图是否为空
本文重点介绍如何在 C++ 中使用映射的优先级队列。在设计复杂的数据结构时,映射的优先级队列非常有用。
地图的最大堆优先级队列:
- 最大堆优先级队列,其中映射按非降序排列:
priority_queue
Here,
data_type1: is the type of data for key.
data_type2: is the type of data for value.
- 最大堆优先级队列,其中映射按非升序排列:
priority_queue
Here,
data_type1: is the type of data for key.
data_type2: is the type of data for value.
地图的最小堆优先级队列:
- 地图按非降序排列的最小堆优先级队列:
priority_queue
greater
Here,
data_type1: is the data type for key.
data_type2: is the data type for value.
- 地图按非升序排列的最小堆优先级队列:
priority_queue
greater
Here,
data_type1: is the data type for key.
data_type2: is the data type for value.
自定义地图优先级队列(使用比较器):
priority_queue
myComparator> priorityQueue;
Here,
data_type1: is the data type for key.
data_type2: is the data type for value.
myComparator: is the comparator class or struct.
下面是比较器的 C++ 代码片段:
C++
// Comparator structure
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// body
}
};
// Comparator class
struct myComparator
{
public:
int operator()(const map& map1,
const map& map2)
{
// body
}
};
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue contents
void print(priority_queue >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 = priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[11] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// 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
void print(priority_queue >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// 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
void print(priority_queue > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map > map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// 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
void print(priority_queue > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map > map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = "Code";
map4[2] = "For";
map4[4] = "Fun";
priorityQueue.push(map4);
// 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
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 = priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// 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
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = "Code";
map4[2] = "For";
map4[4] = "Fun";
priorityQueue.push(map4);
// 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
void print(priority_queue,
vector > >,
greater > > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector > >,
greater > > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map > map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// 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
void print(priority_queue<
map >,
vector > >,
greater > > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue >,
vector > >,
greater > > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map > map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = "Code";
map4[2] = "For";
map4[4] = "Fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Max-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 < value2;
}
return key1 < key2;
}
return map1.size() <= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator>
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Min-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 > value2;
}
return key1 > key2;
}
return map1.size() >= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Max-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 < value2;
}
return key1 < key2;
}
return map1.size() <= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = "apple";
map1[2] = "boy";
map1[3] = "cat";
priorityQueue.push(map1);
// Declaring another map whose key
// is integer type and, value is
// also integer type
map map2;
map2[1] = "apple";
map2[2] = "bat";
map2[4] = "eat";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "apple";
map3[2] = "ao";
map3[10] = "fo";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = "code";
map4[2] = "for";
map4[10] = "fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Min-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 > value2;
}
return key1 > key2;
}
return map1.size() >= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 = priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , "
<< "value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = "apple";
map1[2] = "boy";
map1[3] = "cat";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = "apple";
map2[2] = "bat";
map2[4] = "eat";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "apple";
map3[2] = "ao";
map3[10] = "fo";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = "code";
map4[2] = "for";
map4[10] = "fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
地图的最大堆优先级队列
默认情况下,优先级队列是最大堆。因此,对于最大堆优先级队列中的一对映射,两个映射元素的键和值是同时一个一个地比较的。如果两个映射的第一个元素的键相等,则比较第一个元素的值,如果两个映射的第一个元素的值也相等,则比较第二个元素的键 如果键两个映射的第二个元素的值也相等,然后比较第二个元素的值,依此类推。在比较期间具有较大键或值的映射成为优先级队列的最顶部元素。
以下是 C++ 程序,用于演示映射的最大堆优先级队列的工作原理,以便映射元素按键以非降序排序:
示例 1:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue contents
void print(priority_queue >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 = priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[11] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:2 , value:22 key:4 , value:33 key:11 , value:11 ]
[ key:1 , value:11 key:2 , value:22 key:4 , value:33 ]
[ key:1 , value:11 key:2 , value:22 key:3 , value:44 ]
[ key:1 , value:11 key:2 , value:22 key:3 , value:33 ]
示例 2:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority
// queue contents
void print(priority_queue >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:1 , value:geeks key:2 , value:for key:4 , value:geeks ]
[ key:1 , value:geeks key:2 , value:for key:3 , value:learning ]
[ key:1 , value:geeks key:2 , value:for key:3 , value:geeks ]
Maps的最大堆优先级队列
以下是 C++ 程序,用于演示映射的最大堆优先级队列的工作,以便映射元素按键以非升序排序:
示例 1:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue contents
void print(priority_queue > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map > map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:4 , value:33 key:2 , value:22 key:1 , value:11 ]
[ key:4 , value:33 key:2 , value:22 key:1 , value:11 ]
[ key:3 , value:44 key:2 , value:22 key:1 , value:11 ]
[ key:3 , value:33 key:2 , value:22 key:1 , value:11 ]
示例 2:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue contents
void print(priority_queue > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map > map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = "Code";
map4[2] = "For";
map4[4] = "Fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:4 , value:geeks key:2 , value:for key:1 , value:geeks ]
[ key:4 , value:Fun key:2 , value:For key:1 , value:Code ]
[ key:3 , value:learning key:2 , value:for key:1 , value:geeks ]
[ key:3 , value:geeks key:2 , value:for key:1 , value:geeks ]
地图的最小堆优先级队列
在最小堆优先级队列中有一对map的情况下,两个map元素的key和value同时被一个一个的比较。如果两个映射的第一个元素的键相等,则比较第一个元素的值,如果两个映射的第一个元素的值也相等,则比较第二个元素的键 如果键两个映射的第二个元素的值也相等,然后比较第二个元素的值,依此类推。在比较期间具有较小键或值的映射成为优先级队列的最顶部元素。
以下是 C++ 程序,用于演示映射的最小堆优先级队列的工作,以便映射元素按键以非降序排列:
示例 1:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue contents
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 = priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:1 , value:11 key:2 , value:22 key:3 , value:33 ]
[ key:1 , value:11 key:2 , value:22 key:3 , value:44 ]
[ key:1 , value:11 key:2 , value:22 key:4 , value:33 ]
示例 2:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority queue contents
void print(priority_queue,
vector >,
greater > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
greater > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = "Code";
map4[2] = "For";
map4[4] = "Fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:1 , value:Code key:2 , value:For key:4 , value:Fun ]
[ key:1 , value:geeks key:2 , value:for key:3 , value:geeks ]
[ key:1 , value:geeks key:2 , value:for key:3 , value:learning ]
[ key:1 , value:geeks key:2 , value:for key:4 , value:geeks ]
Maps的最小堆优先级队列
以下是 C++ 程序,用于演示映射的最小堆优先级队列的工作方式,以便映射元素按键以非升序排列:
示例 1:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority
// queue contents
void print(priority_queue,
vector > >,
greater > > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector > >,
greater > > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map > map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:3 , value:33 key:2 , value:22 key:1 , value:11 ]
[ key:3 , value:44 key:2 , value:22 key:1 , value:11 ]
[ key:4 , value:33 key:2 , value:22 key:1 , value:11 ]
[ key:4 , value:33 key:2 , value:22 key:1 , value:11 ]
示例 2:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print priority
// queue contents
void print(priority_queue<
map >,
vector > >,
greater > > >
priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map > map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue >,
vector > >,
greater > > >
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map > map1;
map1[1] = "geeks";
map1[2] = "for";
map1[3] = "geeks";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map2;
map2[1] = "geeks";
map2[2] = "for";
map2[3] = "learning";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map3;
map3[1] = "geeks";
map3[2] = "for";
map3[4] = "geeks";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map > map4;
map4[1] = "Code";
map4[2] = "For";
map4[4] = "Fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:3 , value:geeks key:2 , value:for key:1 , value:geeks ]
[ key:3 , value:learning key:2 , value:for key:1 , value:geeks ]
[ key:4 , value:Fun key:2 , value:For key:1 , value:Code ]
[ key:4 , value:geeks key:2 , value:for key:1 , value:geeks ]
自定义地图优先级队列
以下是演示自定义地图优先级队列工作的 C++ 程序:
示例 1:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Max-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 < value2;
}
return key1 < key2;
}
return map1.size() <= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator>
priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:1 , value:11 key:2 , value:22 key:4 , value:33 ]
[ key:1 , value:11 key:2 , value:22 key:4 , value:33 ]
[ key:1 , value:11 key:2 , value:22 key:3 , value:44 ]
[ key:1 , value:11 key:2 , value:22 key:3 , value:33 ]
示例 2:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Min-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 > value2;
}
return key1 > key2;
}
return map1.size() >= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a map whose key is
// integer type and, value is also
// integer type
map map1;
map1[1] = 11;
map1[2] = 22;
map1[3] = 33;
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = 11;
map2[2] = 22;
map2[3] = 44;
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = 11;
map3[2] = 22;
map3[4] = 33;
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = 11;
map4[2] = 22;
map4[4] = 33;
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:1 , value:11 key:2 , value:22 key:3 , value:33 ]
[ key:1 , value:11 key:2 , value:22 key:3 , value:44 ]
[ key:1 , value:11 key:2 , value:22 key:4 , value:33 ]
[ key:1 , value:11 key:2 , value:22 key:4 , value:33 ]
示例 3:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Max-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 < value2;
}
return key1 < key2;
}
return map1.size() <= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 =
priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , " <<
"value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = "apple";
map1[2] = "boy";
map1[3] = "cat";
priorityQueue.push(map1);
// Declaring another map whose key
// is integer type and, value is
// also integer type
map map2;
map2[1] = "apple";
map2[2] = "bat";
map2[4] = "eat";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "apple";
map3[2] = "ao";
map3[10] = "fo";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = "code";
map4[2] = "for";
map4[10] = "fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:1 , value:code key:2 , value:for key:10 , value:fun ]
[ key:1 , value:apple key:2 , value:boy key:3 , value:cat ]
[ key:1 , value:apple key:2 , value:bat key:4 , value:eat ]
[ key:1 , value:apple key:2 , value:ao key:10 , value:fo ]
示例 4:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
struct myComparator
{
int operator()(const map& map1,
const map& map2)
{
// Min-heap priority queue
for (auto it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end(),
it2 != map2.end();
it1++, it2++)
{
auto key1 = it1->first;
auto value1 = it1->second;
auto key2 = it2->first;
auto value2 = it2->second;
if (key1 == key2)
{
if (value1 == value2)
continue;
return value1 > value2;
}
return key1 > key2;
}
return map1.size() >= map2.size();
}
};
// Function to print priority
// queue contents
void print(priority_queue,
vector >,
myComparator> priorityQueue)
{
while (!priorityQueue.empty())
{
// Each element of the priority
// queue is a map itself
map map1 = priorityQueue.top();
cout << "[ ";
// Print the map elements
for (auto pair1 : map1)
{
// Each element in a map is a key,
// value pair
cout << "key:" << pair1.first << " , "
<< "value:" << pair1.second << " ";
}
cout << ']';
cout << '\n';
// Pop out the topmost map
priorityQueue.pop();
}
}
// Driver code
int main()
{
// Priority queue of map
// Each element of the priority
// queue is a map
priority_queue,
vector >,
myComparator> priorityQueue;
// Declaring a map whose key is
// integer type and, value is
// also integer type
map map1;
map1[1] = "apple";
map1[2] = "boy";
map1[3] = "cat";
priorityQueue.push(map1);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map2;
map2[1] = "apple";
map2[2] = "bat";
map2[4] = "eat";
priorityQueue.push(map2);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map3;
map3[1] = "apple";
map3[2] = "ao";
map3[10] = "fo";
priorityQueue.push(map3);
// Declaring another map whose key is
// integer type and, value is also
// integer type
map map4;
map4[1] = "code";
map4[2] = "for";
map4[10] = "fun";
priorityQueue.push(map4);
// Calling print function
print(priorityQueue);
return 0;
}
输出:
[ key:1 , value:apple key:2 , value:ao key:10 , value:fo ]
[ key:1 , value:apple key:2 , value:bat key:4 , value:eat ]
[ key:1 , value:apple key:2 , value:boy key:3 , value:cat ]
[ key:1 , value:code key:2 , value:for key:10 , value:fun ]