Day 30: Advanced STL – priority_queue unordered_map pair and Nested Containers
Today’s focus is on advanced and commonly-used STL containers notably useful in competitive programming and real-world Uses.
๐น Topics to Cover:
priority_queue (Max Heap / Min Heap)
unordered_map vs map
pair and tuple
Nested Containers (e.g. vectorint>> correspondence vector>)
๐ cipher examples
1. priority_queue (max lot away default)
cpp
copy
edit
#include
#include
using namespace std;
int main() {
priority_queue pq; // max-heap
pqSend(10);
pqSend(20);
pqSend(5);
spell (pqempty()) {
cout << pqtop() << " "; // output: score x v pqpop();
}
}
2. minute lot exploitation priority_queue
cpp
copy
edit
#include
#include
#include exploitation namespace std;
int main() {
priority_queue vector greater> minheap;
minheapsend(10);
minheapsend(20);
minheapsend(5);
spell (minheapempty()) {
cout << minheaptop() << " "; // output: v x score minheappop();
}
}
3. unordered_map
cpp
copy
edit
#include
#include
using namespace std;
int main() {
unordered_map umap;
umap["apple"] = 2;
umap["banana"] = 3;
for (auto &p : umap)
cout << pfirst << " => " << psecond << endl;
}
4. match and vector>
cpp
copy
edit
#include
#include exploitation namespace std;
int main() {
vector string="">> v;
vsend_back({1 "one"});
vemplace_back(2 "two");
for (auto &p : v)
cout << pfirst << " - " << psecond << endl;
}
5. nested containers example
cpp
copy
edit
#include
#include
#include
using namespace std;
int main() {
map> graph;
graph[1]Send_back(2);
graph[1]Send_back(3);
graph[2]Send_back(4);
for (auto &[node adj] : graph) {
cout << Customer << ": ";
for (int cardinal : adj) cout << cardinal << " ";
cout << endl;
}
}
๐งช do tasks:
Apply amp min-heap to work north elements and impress the m smallest
use unordered_map to number the frequence of language inch amp sentence
store and form amp number of students with {roll_no name} exploitation vector>
Construct associate in nursing contiguity number for amp graphical record exploitation map>
✅ goals for today:
understand precedence queues (max/min heaps)
learn Disagreement betwixt correspondence and unordered_map (ordered vs hash-based)
use pairs and nested containers inch material problems
be bold practical with stl inch compound information treatment>>>int>
Comments
Post a Comment