Weird unused static string declaration in C++ code

前端 未结 1 1537
失恋的感觉
失恋的感觉 2020-12-12 06:53

Today in a popular problem solving site, i submitted a solution for a problem, then looked up a more faster code to find out what is the reason that his/her code is running

相关标签:
1条回答
  • 2020-12-12 07:07

    You have guessed most of it:

    • it is executed before the main by initializing a static variable using a lambda
    • it disables i/o related things that will slow down execution:
      • std::ios::sync_with_stdio
      • cin.tie(nullptr) will untie cin from cout. See below
    • initializing a string like this is not costly because it won't cause a heap allocation for an empty string

    from cplusplus.com:

    The tied stream is an output stream object which is flushed before each i/o operation in this stream object

    I guess this problem takes input from cin and you provide the answer on cout, from this point onward, writing to cout will be faster.

    0 讨论(0)
提交回复
热议问题