std::priority_queue (Priority Queue, or Max-Heap)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <queue>
#include <vector>
#include <string>
// Custom object
struct Person {
std::string name;
int age;
};
// Custom comparator: returns true if p1 should come after p2 (i.e., if p1.age is less than p2.age).
// This ensures that the person with the highest age appears at the top of the max heap.
struct ComparePerson {
bool operator()(const Person &p1, const Person &p2) {
return p1.age < p2.age;
}
};
int main() {
// Create a max heap (priority queue) of Person objects using our custom comparator
std::priority_queue<Person, std::vector<Person>, ComparePerson> pq;
// Insert some Person objects into the priority queue
pq.push(Person{"Alice", 30});
pq.push(Person{"Bob", 25});
pq.push(Person{"Charlie", 35});
pq.push(Person{"Diana", 28});
// The top of the priority queue will be the Person with the highest age.
std::cout << "Persons in order of descending age:" << std::endl;
while (!pq.empty()) {
Person top = pq.top();
std::cout << top.name << " (" << top.age << ")" << std::endl;
pq.pop();
}
return 0;
}
top()is O(1),pop()is O(log(N))-
A heap requires
operator<. whenp1 < p2, the heap is a max heap, that is, the top is the maximum value. std::for_each:
1
2
std::vector<int> seq(0, 10);
std::for_each(seq.begin(), seq.end(), [idx = 0u](size_t& i) mutable {i = idx++;})
0uinstead of0is used so the initializer is deduced into size_t, otherwise, it’s int-
Need
mutable, because lambda’soperator()by default is a const:1 2 3 4 5 6 7
struct Lambda{ int idx; // Wrong - need mutable void operator() const{ i = idx++; } };