The STL
map, set, queue and algorithms, essential for coding rounds.
C++
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
map<char, int> counts;
for (char c : string("banana")) {
counts[c]++;
}
for (auto &[ch, n] : counts) {
cout << ch << n << " ";
}
cout << "\n";
set<int> unique = {3, 1, 3, 2};
cout << unique.size() << "\n";
priority_queue<int> pq;
pq.push(4);
pq.push(9);
cout << pq.top() << "\n";
vector<int> v = {1, 2, 3};
reverse(v.begin(), v.end());
cout << v[0] << "\n";
return 0;
}Output
a3 b1 n2 3 9 3
| Container | Good for |
|---|---|
vector | Default choice; fast access by index |
map / set | Sorted; O(log n) lookup |
unordered_map | Average O(1) lookup; no order |
stack / queue | LIFO and FIFO |
priority_queue | Always gives the largest item |