From 5456836741f245ca49ee639c3db7d3e7301c463e Mon Sep 17 00:00:00 2001 From: jxu <7989982+jxu@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:35:07 -0400 Subject: [PATCH 1/4] Create cpp_tips.md --- src/others/cpp_tips.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/others/cpp_tips.md diff --git a/src/others/cpp_tips.md b/src/others/cpp_tips.md new file mode 100644 index 000000000..6cfe9c67e --- /dev/null +++ b/src/others/cpp_tips.md @@ -0,0 +1,22 @@ +--- +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`) 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`. +- `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. +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. + +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 `: this is a catch-all implementation-defined header in g++ that avoids having a list of dozens of includes like `#include `, `#include `, `#include `, etc. It should work on any CP judge site, but it is not a standard header. From 7952d74d27adfbd3cb977b5ac1f1351d31f249e6 Mon Sep 17 00:00:00 2001 From: jxu <7989982+jxu@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:54:50 -0400 Subject: [PATCH 2/4] cpp_tips: mention redefining ints --- src/others/cpp_tips.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/others/cpp_tips.md b/src/others/cpp_tips.md index 6cfe9c67e..dc308bef9 100644 --- a/src/others/cpp_tips.md +++ b/src/others/cpp_tips.md @@ -20,3 +20,8 @@ References: - `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 `: this is a catch-all implementation-defined header in g++ that avoids having a list of dozens of includes like `#include `, `#include `, `#include `, etc. It should work on any CP judge site, but it is not a standard header. +- `#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, be aware of 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`. + - May require twice as much memory than `int` and 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`. From 0c589a48e2cac8fed25c0053cbe10090af16318d Mon Sep 17 00:00:00 2001 From: jxu <7989982+jxu@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:40:20 -0400 Subject: [PATCH 3/4] cpp_tips: note flushing for interactive problems --- src/others/cpp_tips.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/others/cpp_tips.md b/src/others/cpp_tips.md index dc308bef9..b01f87aec 100644 --- a/src/others/cpp_tips.md +++ b/src/others/cpp_tips.md @@ -10,7 +10,7 @@ tags: - `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. 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`. - `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. -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. +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), From edabef5c3b5f1522f9a7596e11a18f25beba522b Mon Sep 17 00:00:00 2001 From: jxu <7989982+jxu@users.noreply.github.com> Date: Mon, 4 Nov 2024 09:34:28 -0500 Subject: [PATCH 4/4] cpp_tips: address review comments https://github.com/cp-algorithms/cp-algorithms/pull/1383 --- src/others/cpp_tips.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/others/cpp_tips.md b/src/others/cpp_tips.md index b01f87aec..85cdbf7f0 100644 --- a/src/others/cpp_tips.md +++ b/src/others/cpp_tips.md @@ -7,7 +7,7 @@ tags: ## Faster I/O -- `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. +- `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`. - `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`. @@ -19,9 +19,9 @@ References: ## 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 `: this is a catch-all implementation-defined header in g++ that avoids having a list of dozens of includes like `#include `, `#include `, `#include `, etc. It should work on any CP judge site, but it is not a standard header. -- `#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, be aware of unexpected issues: +- `#include `: this is a catch-all implementation-defined header in g++ that avoids having a list of dozens of includes like `#include `, `#include `, `#include `, 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`. - - May require twice as much memory than `int` and make the program slower, which may be important in extremely time-critical code. + - 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`. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy