Created
November 13, 2015 16:28
-
-
Save avamsi/cccc17a11961c70afcf9 to your computer and use it in GitHub Desktop.
Fast IO and Python like print statement.
This file contains 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
#include <bits/stdc++.h> | |
using namespace std; | |
namespace __io__ { | |
static struct IN { | |
char buff[1 << 10]; | |
char c; | |
inline operator bool() { | |
return c > 0; | |
} | |
inline char next() { | |
return c = getchar_unlocked(); | |
} | |
inline bool space(const char& c) { | |
return (c > 0 and c < 33); | |
} | |
inline bool skip() { | |
while (space(next())); | |
return c > 0; | |
} | |
inline IN& operator, (char& var) { | |
var = next(); | |
return *this; | |
} | |
inline IN& operator, (char* var) { | |
if (skip()) { | |
*(var++) = c; | |
while (not space(next())) { | |
*(var++) = c; | |
} | |
*(var++) = 0; | |
} | |
return *this; | |
} | |
inline IN& operator, (string& var) { | |
if (skip()) { | |
var.clear(); | |
var += c; | |
while (not space(next())) { | |
var += c; | |
} | |
} | |
return *this; | |
} | |
template <typename T> | |
inline enable_if_t< | |
is_same<T, int16_t>::value or | |
is_same<T, int32_t>::value or | |
is_same<T, int64_t>::value, | |
IN&> operator, (T& var) { | |
if (skip()) { | |
int8_t sign = 1; | |
if (c == '-') { | |
sign = -1; | |
next(); | |
} | |
var = c - '0'; | |
while (not space(next())) { | |
var = 10*var + (c - '0'); | |
} | |
var *= sign; | |
} | |
return *this; | |
} | |
template <typename T> | |
inline enable_if_t< | |
is_same<T, uint16_t>::value or | |
is_same<T, uint32_t>::value or | |
is_same<T, uint64_t>::value, | |
IN&> operator, (T& var) { | |
if (skip()) { | |
var = c - '0'; | |
while (not space(next())) { | |
var = 10*var + (c - '0'); | |
} | |
} | |
return *this; | |
} | |
inline IN& operator, (float& var) { | |
if ((*this), buff) { | |
sscanf(buff, "%f", &var); | |
} | |
return *this; | |
} | |
inline IN& operator, (double& var) { | |
if ((*this), buff) { | |
sscanf(buff, "%lf", &var); | |
} | |
return *this; | |
} | |
inline IN& operator, (long double& var) { | |
if ((*this), buff) { | |
sscanf(buff, "%Lf", &var); | |
} | |
return *this; | |
} | |
template <typename T> | |
inline IN& operator, (vector<T>& var) { | |
for (auto& i: var) { | |
(*this), i; | |
} | |
return *this; | |
} | |
} reader; | |
static struct OUT { | |
char c; | |
char buff[1 << 10]; | |
inline OUT& operator, (const char& var) { | |
putchar_unlocked(var); | |
return *this; | |
} | |
inline OUT& operator, (const char* var) { | |
while (*var) { | |
putchar_unlocked(*var++); | |
} | |
return *this; | |
} | |
inline OUT& operator, (const string& var) { | |
for (auto& i: var) { | |
putchar_unlocked(i); | |
} | |
return *this; | |
} | |
template <typename T> | |
inline OUT& write_i(T var) { | |
int8_t i = 0; | |
do { | |
buff[i++] = var%10 + '0'; | |
var /= 10; | |
} while (var != 0); | |
while (--i >= 0) { | |
putchar_unlocked(buff[i]); | |
} | |
return *this; | |
} | |
template <typename T> | |
inline enable_if_t< | |
is_same<T, int16_t>::value or | |
is_same<T, int32_t>::value or | |
is_same<T, int64_t>::value, | |
OUT&> operator, (const T& var) { | |
if (var < 0) { | |
putchar_unlocked('-'); | |
return write_i(-var); | |
} | |
return write_i(var); | |
} | |
template <typename T> | |
inline enable_if_t< | |
is_same<T, uint16_t>::value or | |
is_same<T, uint32_t>::value or | |
is_same<T, uint64_t>::value, | |
OUT&> operator, (const T& var) { | |
return write_i(var); | |
} | |
inline OUT& operator, (const float& var) { | |
sprintf(buff, "%f%c", var, '\0'); | |
return (*this), buff; | |
} | |
inline OUT& operator, (const double& var) { | |
sprintf(buff, "%lf%c", var, '\0'); | |
return (*this), buff; | |
} | |
inline OUT& operator, (const long double& var) { | |
sprintf(buff, "%Lf%c", var, '\0'); | |
return (*this), buff; | |
} | |
} writer; | |
struct print { | |
bool space = false; | |
~print() { | |
writer, '\n'; | |
} | |
template <typename T> | |
print& operator, (const T& var) { | |
if (space) { | |
writer, ' '; | |
} | |
else { | |
space = true; | |
} | |
writer, var; | |
return *this; | |
} | |
}; | |
} | |
#define read __io__::reader, | |
#define write __io__::writer, | |
#define print __io__::print(), | |
int main() { | |
print "Hello!"; | |
print 1, 1.2, '2'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment