Stage 1 · Lesson 4 of 7
Vectors, pairs and sorting
A vector is a resizable array, the container you will use in almost every problem. A pair holds two values, and compare by the first value and then by the second, so sorting a vector of pairs sorts by one key and keeps a second value, such as the original index, alongside it.
sort runs in O(n log n) and accepts a , a small function (often a , an unnamed inline function) that says which of two elements comes first. Custom orders drive many greedy solutions, so get comfortable writing them.
Common trapwriting a comparator with <= instead of <, which breaks the rules sort relies on and can crash or give nonsense.
A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CourseFreeLearnCpp.com
LearnCpp.com · Beginner
Free and thorough: read the chapters on vectors, references, lambdas and the standard library if C++ is new to you
- BookFreeCompetitive Programmer's Handbook
Antti Laaksonen · Beginner
Chapter 1 covers C++ for contests, input and output and working with numbers; Chapter 2 covers complexity
Practise
Keep handy
Also worth your time
A clear walk through time and space complexity, the language you need to read constraints
- USACO GuideGo deeper
The General section has practical pages on choosing a language, fast input and output and debugging