# C++ for DSA

> Learn the slice of modern C++ that matters for interviews and contests: fast I/O, references, the STL containers and algorithms, and common gotchas. Free...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpp-for-dsa
Last updated: 2026-11-28

Learn the slice of modern C++ that matters for interviews and contests: fast I/O, references, the STL containers and algorithms, and common gotchas.

Beginner · about 20 hours · difficulty 2/5

C++ is the most common language in Indian placement rounds and competitive programming because it is fast, has a rich standard library (the **STL**) and gives you precise control over memory. You do not need all of C++; you need a focused subset that you can write without thinking.

### What to actually learn

- Basics: types (`int`, `long long`, `double`, `char`, `bool`), overflow limits (int is about 2.1e9, long long about 9.2e18), loops, functions.
- **References vs copies**: pass big containers as `const vector<int>& v`. Passing by value copies the whole vector and can turn an O(n) solution into TLE.
- STL containers: `vector`, `string`, `pair`, `array`, `stack`, `queue`, `deque`, `priority_queue`, `set`/`map` (balanced BST, O(log n)), `unordered_set`/`unordered_map` (hash, O(1) average).
- STL algorithms: `sort` with custom comparators or lambdas, `lower_bound`/`upper_bound`, `reverse`, `accumulate`, `max_element`, `next_permutation`.
- Fast I/O: `ios::sync_with_stdio(false); cin.tie(nullptr);` for contests.

### Common pitfalls

- Computing `a * b` in `int` before assigning to `long long` still overflows; cast first.
- `v.size()` is unsigned, so `v.size() - 1` on an empty vector wraps to a huge number.
- `map[key]` inserts a default value if the key is missing; use `count` or `find` to test membership.

Good enough to move on: you can implement a frequency count with `unordered_map`, sort pairs by second element with a lambda, and use a min-heap via `priority_queue<int, vector<int>, greater<int>>` without looking anything up.

## Key concepts

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

## Other options for this step

- [Java for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/java-for-dsa): Use Java and its Collections Framework for DSA: ArrayList, HashMap, TreeMap, PriorityQueue, ArrayDeque, plus fast input and common boxing traps.
- [Python for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/python-for-dsa): Use Python's concise syntax and built-ins (list, dict, set, heapq, deque, bisect, Counter) to solve interview problems quickly, while knowing its performance limits.

## Mini project

Write a reusable contest template, then solve 6 warm-ups using only STL: count word frequencies, sort students by marks then name, find the k largest numbers with a heap, remove duplicates with set, check anagrams with a frequency array, and answer 'first element >= x' queries with lower_bound.

## Check yourself

Why can passing a vector<int> by value inside a recursive function cause a Time Limit Exceeded verdict, and how do you fix it?

## Free resources

- [LearnCpp.com](https://www.learncpp.com/) (LearnCpp)
- [C++ reference](https://en.cppreference.com/w/) (cppreference)
- [Competitive Programmer's Handbook (Ch. 1, 4)](https://cses.fi/book/book.pdf) (CSES)

Next: [Java for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/java-for-dsa)

## More in Your Problem-Solving Toolkit

- [C++ for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpp-for-dsa)
- [Java for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/java-for-dsa)
- [Python for DSA](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/python-for-dsa)
- [Complexity Analysis](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/complexity-analysis)
- [Git and GitHub Workflow](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/git-version-control)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
