Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.8k
Cpp tips and tricks#1383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
jxu wants to merge5 commits intomainChoose a base branch fromcpp-tips
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Cpp tips and tricks#1383
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletionssrc/others/cpp_tips.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
tags: | ||
- Original | ||
--- | ||
# C++ Tips and Tricks for Competition Programming | ||
## Faster I/O | ||
- `std::ios_base::sync_with_stdio(false)`: by default, C++ works with C++ iostreams (like `std::cin`, may not apply to new C++23 `std::print`/`std::println`) and C style stdio streams (like `stdin`) by synchronizing them. | ||
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`. | ||
jxu marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
- `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. | ||
You can disable this automatic flushing to make I/O faster. The exception is for interactive problems with interleaved inputs and outputs, for which you can either not use `cin.tie(0)` or flush explicitly with `cout.flush()`/`cout.endl`. | ||
References: | ||
[Dr. Dobbs - The Standard Librarian: IOStreams and Stdio](https://www.drdobbs.com/the-standard-librarian-iostreams-and-std/184401305), | ||
[Codeforces - Best form of C++ I/O?](https://codeforces.com/blog/entry/6251) | ||
## Less Typing | ||
- `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). | ||
- `#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. On your local machine, it can be pre-compiled to greatly save compilation time. | ||
- `#define int long long`: some people define `int` to always be (at least) 64-bit to avoid overflow issues, since most CP problems use numbers that fit in a 64-bit integer. However, this is **not recommended** because of many unexpected issues: | ||
- C++ requires `main` to be a function returning `int` and not `long long`, so use `signed main` as a workaround. | ||
- Function calls like `min(x, 0)` may complain that `0` is type `int`. | ||
- Requires twice as much memory than `int` and may make the program slower, which may be important in extremely time-critical code. | ||
- Otherwise you can use `typedef long long ll` or use the appropriate fixed-width integer types like `int64_t`. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.