Skip to content

Commit 5456836

Browse files
authored
Create cpp_tips.md
1 parent 820fc4a commit 5456836

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/others/cpp_tips.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
tags:
3+
- Original
4+
---
5+
6+
# C++ Tips and Tricks for Competition Programming
7+
8+
## Faster I/O
9+
10+
- `std::ios_base::sync_with_stdio(false)`: by default, C++ works with C++ iostreams (like `std::cin`) and C style stdio streams (like `stdin`) by synchronizing them.
11+
Since you should never need C style I/O (like `scanf`, `printf`), in C++, disabling this synchronization will make C++ I/O competitive with C style `scanf`.
12+
- `std::cin.tie(0)`: By default, iostreams flush cout every time you read from cin, so that you will always display any output before reading any input.
13+
If you are not solving an interactive problem, i.e. you don't require interleaving inputs and outputs, you can disable this automatic flushing to make I/O faster.
14+
15+
References:
16+
[Dr. Dobbs - The Standard Librarian: IOStreams and Stdio](https://www.drdobbs.com/the-standard-librarian-iostreams-and-std/184401305),
17+
[Codeforces - Best form of C++ I/O?](https://codeforces.com/blog/entry/6251)
18+
19+
## Less Typing
20+
21+
- `using namespace std;`: this saves lots of tedious typing of `std::` before all the standard library functions and objects. However, a blanket namespace import like this can cause name clashes, so don't name your variables or functions something common like `find` or anything from [this list](https://en.cppreference.com/w/cpp/symbol_index).
22+
- `#include <bits/stdc++.h>`: this is a catch-all implementation-defined header in g++ that avoids having a list of dozens of includes like `#include <vector>`, `#include <iostream>`, `#include <string>`, etc. It should work on any CP judge site, but it is not a standard header.

0 commit comments

Comments
 (0)