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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
template <typename T, typename U, typename V>
void
printPQ(std::priority_queue<T, U, V>& q);
template <typename T, typename U, typename V>
std::priority_queue<T, U, V>
addAll(std::priority_queue<T, U, V>& one, std::priority_queue<T, U, V>& two);
int main()
{
std::vector<std::string> v1 = {
"George", "Jim", "John", "Blake", "Kevin", "Michael"
};
std::priority_queue < std::string,
std::vector<std::string>,
std::greater<std::string> > q1(std::greater<std::string>(),
v1);
auto q1_bkp { q1 };
std::cout << "q1:\n";
printPQ(q1_bkp);
std::cout << "\n\n";
std::vector<std::string> v2 {
"George", "Katie", "Kevin", "Michelle", "Ryan"
};
std::priority_queue < std::string,
std::vector<std::string>,
std::greater<std::string> > q2(std::greater<std::string>(),
v2);
auto q2_bkp { q2 };
std::cout << "q2:\n";
printPQ(q2_bkp);
std::cout << "\n\n";
addAll(q1, q2);
std::cout << "q1 + q2:\n";
printPQ(q1);
std::cout << "\n\nFrom higher to lower:\n";
std::priority_queue <std::string> q3;
for(const std::string& e : v1) {
q3.push(e);
}
auto q3_bkp { q3 };
std::cout << "q3:\n";
printPQ(q3_bkp);
std::cout << "\n\n";
std::priority_queue<std::string> q4;
for(const std::string& e : v2) {
q4.push(e);
}
auto q4_bkp { q4 };
std::cout << "q4:\n";
printPQ(q4_bkp);
std::cout << "\n\n";
addAll(q3, q4);
std::cout << "q3 + q4:\n";
printPQ(q3);
std::cout << "\n\n";
}
template <typename T, typename U, typename V>
void
printPQ(std::priority_queue<T, U, V>& q)
{
while(!q.empty()) {
std::cout << q.top() << ' ';
q.pop();
}
}
template <typename T, typename U, typename V>
std::priority_queue<T, U, V>
addAll(std::priority_queue<T, U, V>& one, std::priority_queue<T, U, V>& two)
{
while(!two.empty()) {
one.push(two.top());
two.pop();
}
return one;
}
|