1.C++IO Stream synchronization and binding

stay C++ in ,cin and cout It's not really slow ,C++ Stream in IO It's quite fast , Its speed is related to the initial set buffer size and the hard disk's IO Speed dependent .

But in the C++ in , For compatibility C Of IO(scanf and printf),cin and cout Is set to and C Of IO synchronization , This leads to cin and cout Not as fast as scanf and printf
fast .

in addition , By default , Standard input device (cin) And standard output device (cout) Bound together , It ensures that the output buffer has been flushed before the input is called , This will also reduce the reading efficiency .

therefore , We can cancel them manually C language IO Synchronization of , And remove it cin and cout Binding of , Speed up reading , send cin The speed of scanf not much difference between , Even faster than in some cases scanf.

notes : After unbinding , use scanf and printf There may be problems , Such as read failure or premature output , Please do not use them again , But it can be used getchar and gets
Equal input function .

Acceleration code :
 
  // cancel C++ IO And C Synchronization of
  std::ios::sync_with_stdio(false);
   
  // cancel cin and cout Binding of
  std::cin.tie(NULL);/* Equivalent to cin.tie(0);*/
  

2. Read it quickly

When reading a large number of integers , No matter what cin still scanf, Duby getchar Reading efficiency is much slower , We can use it getchar
To simulate the input function , It is very efficient to read a large number of integers :

One of the implementation methods is given below :
 
  inline void read(int& x) {
  x = 0;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
  ;
  for (; c >= '0' && c <= '9'; c = getchar())
  x = (x << 3) + (x << 1) + c - '0';
  }
  

3. interactive oj( as leetcode) Acceleration method of

Put it in the solution Class , Not in general .
 
  static auto __=[](){
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return nullptr;
  }();
  // Pay attention to the solution above
  

Technology
©2019-2020 Toolsou All rights reserved,
QT Delete directory and file Java aggregate ------LinkedHashMap Underlying principle I've been drinking soft water for three years ? What is the use of soft water and water softener Java Xiao Ming A+B About wechat payment body( Product description ) Problems encountered Digital rolling lottery program 3 species Python data structure ,13 Creation methods , This is the conclusion , Great ! Huawei Mate 40 Pro+ 5G exposure : Leica film lens , Ceramic body What's the difference in server prices ? Operators Website visitors' mobile phone number capture