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,
Final review of database : Summary of comprehensive application questions Laplance operator ( Second derivative ) Simple learning of computer composition principle pyqt Button call python program _PyQt: Link button to function in program How much can you go up once you change jobs ? Today, I saw the ceiling of job hopping python in str Function usage _python in str Usage Summary of built-in functions MySQL trigger web The server nginx---linux Installation and deployment C++ Chapter V polymorphism exercises :( It's coming to an end )python Check built-in functions , How to check python Built in function