Optimized Input and Output in C++

Background

C++标准库<iostream>中的cin, cout相比<cstdio>中的scanf, printf要方便许多,但速度也较为缓慢。在输入输出的数据类型不是很特殊时,可以使用以下代码来代替cin, cout(要包含<cctype>, <cstdio>,不要包含<iostream>)。

Code

class InputStream {
private:
  static int const BUFFER_SIZE = 10000000;
  char *buffer;

public:
  InputStream() {
    buffer = new char[BUFFER_SIZE]();
    fread(buffer, sizeof(char), BUFFER_SIZE, stdin);
  }
  InputStream &operator>>(int &n) {
    for (; *buffer < '0' || *buffer > '9'; ++buffer)
      ;
    for (n = 0; *buffer >= '0' && *buffer <= '9';
         (n *= 10) += *(buffer++) - '0')
      ;
    return *this;
  }
  InputStream &operator>>(char &c) {
    for (; !isprint(*buffer) || *buffer == ' '; ++buffer)
      ;
    c = *(buffer++);
    return *this;
  }
  InputStream &operator>>(char *s) {
    char *t = s;
    for (; !isprint(*buffer) || *buffer == ' '; ++buffer)
      ;
    for (; isprint(*buffer) && *buffer != ' '; *(t++) = *(buffer++))
      ;
    *t = '\0';
    return *this;
  }
} cin;

class OutputStream {
private:
  static int const BUFFER_SIZE = 10000000;
  char *buffer, *cur;

public:
  OutputStream() { buffer = cur = new char[BUFFER_SIZE](); }
  ~OutputStream() { fwrite(buffer, sizeof(char), cur - buffer, stdout); }
  OutputStream &operator<<(int const &n) {
    for (sprintf(cur, "%d", n); *cur; ++cur)
      ;
    return *this;
  }
  OutputStream &operator<<(char const &c) {
    *(cur++) = c;
    return *this;
  }
  OutputStream &operator<<(char *s) {
    for (char *t = s; *t; *(cur++) = *(t++))
      ;
    return *this;
  }
} cout;

static char const endl = '\n';

Limits in C

Limits on Integer Constants

Constant Meaning Value
CHAR_BIT Number of bits in the smallest variable that is not a bit field. 8
SCHAR_MIN Minimum value for a variable of type signed char. –128
SCHAR_MAX Maximum value for a variable of type signed char. 127
UCHAR_MAX Maximum value for a variable of type unsigned char. 255 (0xff)
CHAR_MIN Minimum value for a variable of type char. –128; 0 if /J option used
CHAR_MAX Maximum value for a variable of type char. 127; 255 if /J option used
MB_LEN_MAX Maximum number of bytes in a multicharacter constant. 5
SHRT_MIN Minimum value for a variable of type short. –32768
SHRT_MAX Maximum value for a variable of type short. 32767
USHRT_MAX Maximum value for a variable of type unsigned short. 65535 (0xffff)
INT_MIN Minimum value for a variable of type int. –2147483648
INT_MAX Maximum value for a variable of type int. 2147483647
UINT_MAX Maximum value for a variable of type unsigned int. 4294967295 (0xffffffff)
LONG_MIN Minimum value for a variable of type long. –2147483648
LONG_MAX Maximum value for a variable of type long. 2147483647
ULONG_MAX Maximum value for a variable of type unsigned long. 4294967295 (0xffffffff)
_I64_MIN Minimum value for a variable of type __int64 -9223372036854775808
_I64_MAX Maximum value for a variable of type __int64 9223372036854775807
_UI64_MAX Maximum value for a variable of type unsigned __int64 18446744073709551615 (0xffffffffffffffff)

Limits on Floating-Point Constants

Constant Meaning Value
FLT_DIG DBL_DIG LDBL_DIG Number of digits, q, such that a floating-point number with q decimal digits can be rounded into a floating-point representation and back without loss of precision. 6 15 15
FLT_EPSILON DBL_EPSILON LDBL_EPSILON Smallest positive number x, such that x + 1.0 is not equal to 1.0. 1.192092896e–07F 2.2204460492503131e–016 2.2204460492503131e–016
FLT_GUARD 0
FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG Number of digits in the radix specified by FLT_RADIX in the floating-point significand. The radix is 2; hence these values specify bits. 24 53 53
FLT_MAX DBL_MAX LDBL_MAX Maximum representable floating-point number. 3.402823466e+38F 1.7976931348623158e+308 1.7976931348623158e+308
FLT_MAX_10_EXP DBL_MAX_10_EXP LDBL_MAX_10_EXP Maximum integer such that 10 raised to that number is a representable floating-point number. 38 308 308
FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP Maximum integer such that FLT_RADIX raised to that number is a representable floating- point number. 128 1024 1024
FLT_MIN DBL_MIN LDBL_MIN Minimum positive value. 1.175494351e–38F 2.2250738585072014e–308 2.2250738585072014e–308
FLT_MIN_10_EXP DBL_MIN_10_EXP LDBL_MIN_10_EXP Minimum negative integer such that 10 raised to that number is a representable floating- point number. –37

–307

–307

FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP Minimum negative integer such that FLT_RADIX raised to that number is a representable floating-point number. –125

–1021

–1021

FLT_NORMALIZE 0
FLT_RADIX _DBL_RADIX _LDBL_RADIX Radix of exponent representation. 2 2 2
FLT_ROUNDS _DBL_ROUNDS _LDBL_ROUNDS Rounding mode for floating-point addition. 1 (near) 1 (near) 1 (near)

Data Type Ranges in C

Type Name Bytes Other Names Range of Values
int 4 signed -2,147,483,648 to 2,147,483,647
unsigned int 4 unsigned 0 to 4,294,967,295
__int8 1 char -128 to 127
unsigned __int8 1 unsigned char 0 to 255
__int16 2 short, short int, signed short int -32,768 to 32,767
unsigned __int16 2 unsigned short, unsigned short int 0 to 65,535
__int32 4 signed, signed int, int -2,147,483,648 to 2,147,483,647
unsigned __int32 4 unsigned, unsigned int 0 to 4,294,967,295
__int64 8 long long, signed long long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned __int64 8 unsigned long long 0 to 18,446,744,073,709,551,615
bool 1 none false or true
char 1 none -128 to 127 by default

0 to 255 when compiled by using /J

signed char 1 none -128 to 127
unsigned char 1 none 0 to 255
short 2 short int, signed short int -32,768 to 32,767
unsigned short 2 unsigned short int 0 to 65,535
long 4 long int, signed long int -2,147,483,648 to 2,147,483,647
unsigned long 4 unsigned long int 0 to 4,294,967,295
long long 8 none (but equivalent to __int64) -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long 8 none (but equivalent to unsigned __int64) 0 to 18,446,744,073,709,551,615
enum varies none
float 4 none 3.4E +/- 38 (7 digits)
double 8 none 1.7E +/- 308 (15 digits)
long double same as double none Same as double
wchar_t 2 __wchar_t 0 to 65,535