Look at the top performers of ICPC, IOI and Codeforces and you will see that the vast majority use C++. Why C++ and not modern Python, Go or Java?
The answer is simple: C++ runs very close to the hardware, gives you full control over memory, and its STL (Standard Template Library) offers almost every ready-made data structure at maximum speed.
In this article we reveal the C++ and STL secrets every competitive programmer must know.
1. Fast I/O: speed up input and output 10x
The standard std::cin and std::cout are synchronised with C's stdio (scanf/printf) and flush the buffer on every output (flush).
If the input contains $10^5$ or $10^6$ numbers, plain cin >> n and cout << n << endl are guaranteed to give Time Limit Exceeded (TLE)!
The optimal solution:
#include <iostream>
using namespace std;
int main() {
// 1. Turn off synchronisation with C stdio
ios_base::sync_with_stdio(false);
// 2. Untie cin from cout
cin.tie(NULL);
// 3. Always use '\n' instead of endl!
// endl flushes the buffer every time and throws away the benefit of buffering.
int n;
if (cin >> n) {
cout << n * 2 << '\n';
}
return 0;
}
2. Time complexity of STL containers and the right choice
Many programmers get TLE because they pick the wrong container:
| Container | Search | Insert | Delete | Notes |
|---|---|---|---|---|
std::vector |
$O(N)$ | $O(1)$ amortised | $O(N)$ | The fastest, high cache locality |
std::deque |
$O(N)$ | $O(1)$ at front/back | $O(1)$ | When you need to add at both the front and the back |
std::set / map |
$O(\log N)$ | $O(\log N)$ | $O(\log N)$ | Red-Black Tree, always sorted |
std::unordered_set/map |
$O(1)$ average | $O(1)$ average | $O(1)$ average | Hash table. Can degrade to $O(N)$ in the worst case |
std::priority_queue |
$O(1)$ (top) | $O(\log N)$ | $O(\log N)$ (pop) | Binary Heap. The largest (or smallest) element is always on top |
[!TIP] If you know the number of elements of a
std::vectorin advance, always callv.reserve(N). It brings the cost of repeatedly growing the array down to zero!
3. Make good use of the built-in algorithms (<algorithm>)
Do not reinvent the wheel. The C++ algorithms library already has almost every operation you need, optimised nearly to assembler level:
- Sorting:
sort(v.begin(), v.end())— the Introsort algorithm, always with an $O(N \log N)$ guarantee. - Binary Search:
lower_bound(v.begin(), v.end(), x)— an iterator to the first element with value $\ge x$.upper_bound(v.begin(), v.end(), x)— an iterator to the first element with value $> x$.
- Maximum / minimum:
*max_element(v.begin(), v.end()),min({a, b, c, d}). - Permutations:
next_permutation(v.begin(), v.end())— the next permutation in lexicographic order.
4. Custom comparators and lambda functions
When sorting complex structures, lambda expressions keep the code clean and very fast:
struct Student {
string name;
int score;
int penalty;
};
// Sort by score descending; on a tie, by penalty ascending
sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
if (a.score != b.score) {
return a.score > b.score;
}
return a.penalty < b.penalty;
});
Conclusion
C++ and STL give you an unbeatable weapon in competitive programming. To test your knowledge in practice and raise your rating with fast C++ solutions, head to the Problems section on ALGOR and start practising today!