-2

So I have recently seen the following code optimized for reading numbers. In this case, I am reading a number T (denoting the number of strings that will follow). After this number, T strings follow. However the code instead of printing strings prints out newline characters. If I used cin to read T the code works as expected.

Can some provide some insight into this behavior?

Sample Input

#include <stdio.h>
#include <string>
#include <iostream>

template <typename T>
inline void read(T &f) {
    f = 0; T fu = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
    while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
    f *= fu;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cout.tie(nullptr), std::cin.tie(nullptr);
    int T; 
    read(T);
    while (T--) {
        std::string s;
        std::cin >> s;
        std::cout << s << "\n";
    }
    return 0;
}

Edit: Solution. Do not use std::ios::sync_with_stdio(false); if you are going to intermix C style reading and C++ style reading.

  • 6
    You uncoupled the C-style IO from the C++-style IO, then you decided to intermix the two anyway. I'm not surprised that it doesn't work. – Nathan Pierson Jun 29 '22 at 17:09
  • 1
    Are you being naughty by not checking the return value of `std::cin >> s`? – Bathsheba Jun 29 '22 at 17:11
  • [A note on `inline`](https://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method) – user4581301 Jun 29 '22 at 17:23
  • *"optimized for reading numbers"* My eyes. :( Did you profile if it's actually faster than `cin >> x`? – HolyBlackCat Jun 29 '22 at 17:24
  • @HolyBlackCat Are you suggesting I should stop replacing `x *= 10;` with the optimal `x = x << 3 + x + x;` – user4581301 Jun 29 '22 at 17:25
  • LOL! The bits manipulation might be excessive. But if you are interested here is the article: https://1-oi--wiki-org.translate.goog/contest/io/?_x_tr_sl=zh-CN&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=sc&_x_tr_enc=1 – James Eade Jun 29 '22 at 17:27
  • That's one of Sam's canned comments. Read it as a warning for those who ARE using competition sites to learn C++. There are a lot of them. And they are wasting their time. If you already have a reasonable amount of skill with C++ and are competing to have fun, it doesn't apply to you. – user4581301 Jun 29 '22 at 17:45
  • 2
    *"Are you suggesting I should stop replacing ... with the optimal ..."* Exactly. Coming up with bit manipulation hacks like this is the job of your compiler, not yours. I can guarantee that it's better at it than you are. Ultimately, you should profile both (in release mode), but until you've done so, stick to whatever is easier to read. – HolyBlackCat Jun 29 '22 at 17:47
  • @user4581301 Fair enough. I agree with this. Competition sites are not a method to learn C++. Competition sites do not even require a lot of C++ knowledge [link](https://www.quora.com/In-what-order-should-I-learn-C++-topics-to-get-better-at-competitive-programming). – James Eade Jun 29 '22 at 17:49
  • Where this is (mostly) headed is don't get too smart before you know you have to. I've seen a lot of people, been guilty myself more times than I'm comfortable with, sink massive amounts of time hand-optimizing parts of code that didn't matter. Got crushed at an ACM tournament because the guy we had working the keyboard got hung up hyperoptimizing one of the problems instead of typing in the other problems. It didn't matter how fast the problem ran nearly as much as how fast we submitted solutions that were fast enough. Fast enough should always be the target. – user4581301 Jun 29 '22 at 17:52
  • @user4581301 But it has nothing to do with the topics in this post and provides no intellectual value. It is a response from someone who has a strong negative bias towards competitive programming. Put this comment in an opinion thread where it belongs, not as a answer to this question. – James Eade Jun 29 '22 at 17:54
  • Unrelated: Reading through more of that link on optimized IO, the writer's a bit behind the times on how compilers think. For example, the `register` keyword was deprecated in 2017 and will probably be removed from the language in an upcoming Standard revision because [it's been effectively whitespace](https://www.drdobbs.com/keywords-that-arent-or-comments-by-anoth/184403859) for the last couple decades. There's probably good information in there, but you'll have to pick and choose and run a few of your own tests to see what really sped things up and what didn't. – user4581301 Jun 29 '22 at 18:07
  • @user4581301 I see. Thanks this will be a long detailed reading. – James Eade Jun 29 '22 at 18:11
  • tldr: Computers are cheap and getting faster and faster. Programmers are expensive and have a habit of leaving for other jobs. That means that code needs to be readable to humans other than the original programmer, and not necessarily the fastest it could be. Delegate the latter to the compiler. – Bathsheba Jun 29 '22 at 18:32
  • @SamVarshavchik: Love the comment. – Bathsheba Jun 29 '22 at 18:35

1 Answers1

0

Solution. Do not use std::ios::sync_with_stdio(false); if you are going to intermix C-style reading and C++-style reading.

For more information, see this: Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770