STL and Java Collections
The standard containers in C++ (STL) and Java side by side, what each operation costs, and the pitfalls that cause wrong answers and timeouts.
Why containers
A container is a ready-made data structure from the standard library: C++ calls its collection the STL (Standard Template Library), Java calls its one the Collections Framework. Using them saves time in a contest and removes bugs. The skill is knowing which one fits and what it costs.
| Need | C++ | Java |
|---|---|---|
| growable array | vector<int> | ArrayList<Integer> or int[] |
| sorted set | set<int> | TreeSet<Integer> |
| sorted map | map<int,int> | TreeMap<Integer,Integer> |
| hash set / map | unordered_set, unordered_map | HashSet, HashMap |
| repeated values, sorted | multiset<int> | TreeMap<Integer,Integer> as counts |
| heap (top item fast) | priority_queue<int> (max) | PriorityQueue<Integer> (min) |
| stack, queue, deque | stack, queue, deque | ArrayDeque<Integer> |
1 of 8