teacher
Sign in

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.

30 minCF 800–12006 interactive12 problems

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.

NeedC++Java
growable arrayvector<int>ArrayList<Integer> or int[]
sorted setset<int>TreeSet<Integer>
sorted mapmap<int,int>TreeMap<Integer,Integer>
hash set / mapunordered_set, unordered_mapHashSet, HashMap
repeated values, sortedmultiset<int>TreeMap<Integer,Integer> as counts
heap (top item fast)priority_queue<int> (max)PriorityQueue<Integer> (min)
stack, queue, dequestack, queue, dequeArrayDeque<Integer>
1 of 8