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
ContainerGood for
vectorDefault choice; fast access by index
map / setSorted; O(log n) lookup
unordered_mapAverage O(1) lookup; no order
stack / queueLIFO and FIFO
priority_queueAlways gives the largest item
โ† Inheritance and virtual functionsC++ quick reference โ†’