Блог пользователя platelet

Автор platelet, история, 3 года назад, По-английски

Wrong answer submission and Accepted submission

You can compare them, the only difference is that the Wrong answer submission has an extra cin.tie(0)->sync_with_stdio(0);

I didn't use cin and cout, just fread and fwrite.

Can someone tell me if there is something wrong with my usage or a compiler bug.

  • Проголосовать: нравится
  • +28
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

https://cplusplus.com/reference/ios/ios/tie/

The second form (2) ties the object to tiestr and returns a pointer to the stream tied before the call, if any.

In other words, cin.tie does not return &cin for ->sync_with_stdio(0) to operate on. Instead, it returns the C stdin. You can confirm this by running the program

#include <iostream>

using namespace std;

int main() {
    cout << "cin address = " << (&cin) << endl;
    cout << "cie.tie address = " << cin.tie(0) << endl;
}

For me, it output

cin address = 0x4660
cin.tie address = 0x4710

EDIT: This was not the error, see below.

»
3 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

One way of fixing this is by making dummy methods in your IO struct, and defining cin and cout as macros to your IO struct. For example, my IO struct in this submission uses the same idea and functions as a drop-in replacement for my template that has cin.tie(nullptr)->sync_with_stdio(false) for all practical purposes (other than interactive problems, of course).