Блог пользователя Ahnaf.Shahriar.Asif

Автор Ahnaf.Shahriar.Asif, история, 6 лет назад, По-английски

I saw someone to use a function instead of cin/scanf to input a number;

like that :

int n;
n = in();

where in() function is given bellow :

template <typename T> T in(){char ch;T n = 0;bool ng = false;while (1){ch =getchar();if (ch == '-'){ng = true;ch = getchar();break;}if (ch>='0' && ch<='9')break;}while (1){n = n*10 + (ch - '0');ch = getchar();if (ch<'0' || ch>'9')break;}return (ng?-n:n);}  ?

i don't know what is faster , i also don't know , how scanf or cin works , can anyone please tell me ????

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

»
6 лет назад, # |
  Проголосовать: нравится +19 Проголосовать: не нравится

I wont help you :D :P

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Almost no one knows. Use cin with ios_base::sync_with_stdio(0) and don't think about that

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

The weird looking in function should be faster otherwise what's the point of inventing it?

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

This function should be faster than scanf/cin because it is using getchar() to read data. getchar() is a function in stdio.h that reads and returns a single character from the input, including whitespaces.So what the function in() here is doing is it reads numbers digit by digit and uses them to generate the input number as an int. There also exists a faster version of getchar(), getchar_unlocked() but it does not work in some systems (like Windows). Generally, in terms of speed: getchar_unlocked > getchar > scanf > cin.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    okh , it was fine , but why people downvote me ?? was the question wired ??

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      Yes :D

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

      No one wants to waste their time to understand unstructured questions as yours. What even means this title "Help me please :D", and code is not formated as well. But yet I used my time to point out your mistakes.

      How do you expect someone else to spend more time answering you, when you did not invest any.