amsen's blog

By amsen, history, 8 years ago, In English

I see lots of problems about cout/cin , most of them are telling that cin/cout are slow and it's better to use scanf/printf;

but cin/cout are prettier and more easy to code for c++ coders. so i try to make an IO that it's pretty and fast.


#include <bits/stdc++.h> using namespace std; class FastIO{ public: //Output operators : inline FastIO & operator << (const int &a){ printf("%d" , a); return *this; } inline FastIO & operator << (const long long &a){ printf("%I64d" , a); return *this; } inline FastIO & operator << (const double &a){ printf("%.9f" , a); return *this; } inline FastIO & operator << (const long double &a){ printf("%.9lf" , a); return *this; } inline FastIO & operator << (const char * const&a){ printf("%s" , a); return *this; } inline FastIO & operator << (const string &a){ printf("%s" , a.c_str()); return *this; } //Input operators : inline FastIO & operator >> (int &a){ scanf("%d" , &a); return *this; } inline FastIO & operator >> (long long &a){ scanf("%I64d" , &a); return *this; } inline FastIO & operator >> (double &a){ scanf("%lf" , &a); return *this; } inline FastIO & operator >> (long double &a){ scanf("%lf" , &a); return *this; } inline FastIO & operator >> (char * const&a){ scanf("%s" , a); return *this; } }fastIO;//you can change it to cin/cout int main(){ int a;long long b;double c; fastIO >> a >> b >> c << a << " , " << b << " , " << c << "\n"; }
  • Vote: I like it
  • +16
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Auto comment: topic has been updated by amsen (previous revision, new revision, compare).

»
8 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Auto comment: topic has been updated by amsen (previous revision, new revision, compare).

»
8 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Could be useful for some, but unfortunately this is a poor replacement for the standard input and output streams that C++ provide.

  1. It's missing lots of data types like char, bool, short and all the unsigned variants. Also, missing useful things like endl, hex and other useful stream modifiers. Generally, it's too inflexible: any problem with unique input/output format requirements makes this quite unusable.

  2. There's compatibility issues. "%I64d" is not cross platform, some compilers only accept "%lld". There's probably issues with compatibility with long double, too.

  3. For the output stream there's no point doing const references to data types like int, pass those simply by value. Only objects like std::string should be passed as const &.

While your intent is good, using this implementation as a cout/cin replacement would only restrict yourself and as such developing a dependence on this would not be a good idea.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    1- You can add what you want, i think there is enough data types for coding in codeforces 2- In codeforces it works ;D 3- const reference is faster than normal because it do not copy the value to another variable.

    thanks.

»
8 years ago, # |
Rev. 7   Vote: I like it +10 Vote: I do not like it

Maybe it is faster to read input.

template <class T> inline T IN(T &num)
{
	num = 0;
	char c = getchar(), f = 0;
	while(c != '-' && (c < '0' || c > '9')) c = getchar();
	if(c == '-') f = 1, c = getchar();
	while('0' <= c && c <= '9') num = num * 10 + c - '0', c = getchar();
	if(f) num = -num;
	return num;
}

int main ()
{

	IN(x);

}
»
8 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Maybe it is faster to read input.

template <class T> inline T IN(T &num)
{
	num = 0;
	char c = getchar(), f = 0;
	while(c != '-' && (c < '0' || c > '9')) c = getchar();
	if(c == '-') f = 1, c = getchar();
	while('0' <= c && c <= '9') num = num * 10 + c - '0', c = getchar();
	if(f) num = -num;
	return num;
}