From 513a7d3a7712630a3fd87406a2595a1d0b1c0ca9 Mon Sep 17 00:00:00 2001 From: Nishant Sharma <54657882+nisshar@users.noreply.github.com> Date: Sat, 11 Jan 2020 00:26:12 +0530 Subject: [PATCH] added fast_integer_input.cpp (#696) * added fast_integer_input.cpp * fixed white spaces * fixed white spaces * fixed std:: * fixed std:: * \n Co-authored-by: Christian Clauss --- others/fast_interger_input.cpp | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 others/fast_interger_input.cpp diff --git a/others/fast_interger_input.cpp b/others/fast_interger_input.cpp new file mode 100644 index 000000000..3358fc8bb --- /dev/null +++ b/others/fast_interger_input.cpp @@ -0,0 +1,36 @@ +// Read integers in the fastest way in c plus plus +#include +void fastinput(int *number) { +// variable to indicate sign of input integer + bool negative = false; + register int c; + *number = 0; + + // extract current character from buffer + c = std::getchar(); + if (c == '-') { + // number is negative + negative = true; + + // extract the next character from the buffer + c = std::getchar(); + } + + // Keep on extracting characters if they are integers + // i.e ASCII Value lies from '0'(48) to '9' (57) + for (; (c > 47 && c < 58); c = std::getchar()) + *number = *number *10 + c - 48; + + // if scanned input has a negative sign, negate the + // value of the input number + if (negative) + *(number) *= -1; +} + +// Function Call +int main() { + int number; + fastinput(&number); + std::cout << number << "\n"; + return 0; +}