cpp-algo-cases/test/priority_queue_demo.cpp

28 lines
500 B
C++

#include <queue>
#include <iostream>
struct Person {
std::string name;
int age;
};
struct CompareByAge {
bool operator()(const Person& p1, const Person& p2) const {
return p1.age < p2.age;
}
};
int main() {
std::priority_queue<Person, std::vector<Person>, CompareByAge> pq;
pq.push({"Alice", 25});
pq.push({"Bob", 30});
pq.push({"Charlie", 20});
while (!pq.empty()) {
std::cout << pq.top().name << " ";
pq.pop();
}
return 0;
}