# C++ and the STL

> Use C++ with the Standard Template Library: the fastest and most common choice for placements and competitive programming. Free notes, key concepts, resources...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/cpp-stl
Last updated: 2026-11-28

Use C++ with the Standard Template Library: the fastest and most common choice for placements and competitive programming.

Beginner · about 15 hours · difficulty 2/5

Learn first: [How Campus and Off-Campus Hiring Works](https://www.internhack.in/roadmaps/tracks/dsa-placement/placement-landscape)

C++ is the default language for competitive programming and most Indian placement OAs. It is fast, so slightly heavier solutions still pass, and the **STL** gives you every data structure you need.

### The subset you need

- Types and limits: `int` holds about 2.1e9, `long long` about 9.2e18. Use `long long` for sums and products.
- Containers: `vector`, `string`, `pair`, `stack`, `queue`, `deque`, `priority_queue`, `set`, `multiset`, `map`, `unordered_map`, `unordered_set`.
- Algorithms: `sort` with lambdas, `lower_bound`, `upper_bound`, `reverse`, `accumulate`, `next_permutation`, `__gcd` or `std::gcd`.
- Pass large containers by reference: `const vector<int>& a`.
- Fast I/O: `ios::sync_with_stdio(false); cin.tie(nullptr);`

### Pitfalls

- `a * b` computed in `int` overflows before it is stored into a `long long`. Cast one side first.
- `v.size() - 1` on an empty vector wraps to a huge unsigned number.
- `mp[key]` inserts a default entry; use `count` or `find` to test membership.
- `unordered_map` can be hacked to O(n) per operation on Codeforces; use `map` or a custom hash there.

You are ready to move on when you can write a frequency map, sort pairs by second value with a lambda and build a min-heap without looking anything up.

## Key concepts

- **Types, overflow and long long**
- **vector, string, pair**
- **set, map, multiset**: Ordered, O(log n)
- **unordered_map and unordered_set**
- **stack, queue, deque, priority_queue**
- **sort with lambdas and comparators**
- **lower_bound and upper_bound**
- **Fast I/O and contest template**

## Other options for this step

- [Java and the Collections Framework](https://www.internhack.in/roadmaps/tracks/dsa-placement/java-collections): Use Java with ArrayList, HashMap, ArrayDeque and PriorityQueue: a strong choice if your college teaches Java or you target Java backend roles.
- [Python for DSA](https://www.internhack.in/roadmaps/tracks/dsa-placement/python-for-dsa): Use Python with collections, heapq and bisect: the quickest language to write, fine for interviews but sometimes too slow for strict OAs.

## Mini project

Write a contest template, then solve 8 warm-ups using only STL: word frequency, sort students by marks then name, k largest with a heap, remove duplicates with set, anagram check, first element >= x with lower_bound, next permutation, and running median with two heaps.

## Check yourself

Why can passing a vector<int> by value into a recursive function cause TLE, and what is the one-word fix?

## Free resources

- [cppreference: containers library](https://en.cppreference.com/w/cpp/container) (cppreference)
- [LearnCpp.com](https://www.learncpp.com/) (LearnCpp)
- [Competitive Programmer's Handbook](https://cses.fi/book/book.pdf) (CSES)

Previous: [How Campus and Off-Campus Hiring Works](https://www.internhack.in/roadmaps/tracks/dsa-placement/placement-landscape) · Next: [Java and the Collections Framework](https://www.internhack.in/roadmaps/tracks/dsa-placement/java-collections)

## More in Getting Started: Placements, Language and Complexity

- [How Campus and Off-Campus Hiring Works](https://www.internhack.in/roadmaps/tracks/dsa-placement/placement-landscape)
- [C++ and the STL](https://www.internhack.in/roadmaps/tracks/dsa-placement/cpp-stl)
- [Java and the Collections Framework](https://www.internhack.in/roadmaps/tracks/dsa-placement/java-collections)
- [Python for DSA](https://www.internhack.in/roadmaps/tracks/dsa-placement/python-for-dsa)
- [Time and Space Complexity](https://www.internhack.in/roadmaps/tracks/dsa-placement/complexity-analysis)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
