getchar_unlocked()VS scanf()VS cin [英] getchar_unlocked( ) VS scanf() VS cin

查看:208
本文介绍了getchar_unlocked()VS scanf()VS cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这三种输入函数在编程语言中有什么区别。
他们以不同的方式彼此输入?



1.getchar_unlocked()

  #define getcx getchar_unlocked 

inline void inp(int& n)
{
n = 0;
int ch = getcx(); int sign = 1;
while(ch <'0'|| ch>'9'){if(ch ==' - ')sign = -1; ch = getcx();}

while(ch> ='0'&& ch< ='9')
n =(n < n <1)+ ch-'0',ch = getcx();
n = n * sign;
}

2.scanf(%d,& ; n)



3.cin>> n



在输入整数时最少需要多少时间?



我使用c ++中的头文件,

 #include< iostream> 
#include< vector>
#include< set>
#include< map>
#include< queue>
#include< stack>
#include< string>
#include< algorithm>
#include< functional>
#include< iomanip>
#include< cstdio>
#include< cmath>
#include< cstring>
#include< cstdlib>
#include< cassert>


解决方案

需要考虑两点。
$ b


    getchar_unlocked 在Windows中已弃用,因为它是除非速度因子太多,否则请尝试避免 getchar_unlocked


现在,就速度而言。

  getchar_unlocked> getchar 

因为在 getchar_unlocked 这使它不安全。

  getchar> scanf 

因为 getchar 输入是char类型,而scanf可以读取c中可用的大多数原始类型。

  scanf> cin(>> operator)

因为选中此链接



最后

  getchar_unlocked> getchar> scanf> cin 


What is the difference among these three input functions in programming language. Do they input in different ways from each other?

1.getchar_unlocked()

 #define getcx getchar_unlocked

 inline void inp( int &n ) 
 {
    n=0;
    int ch=getcx();int sign=1;
    while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getcx();}

    while(  ch >= '0' && ch <= '9' )
            n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
    n=n*sign;
  }   

2.scanf("%d",&n)

3.cin>>n

Which one takes least time when input the integers?

I use THese header files in c++ where all 3 cased run in c++;

  #include<iostream>
  #include<vector>
  #include<set>
  #include<map>
  #include<queue>
  #include<stack>
  #include<string>
  #include<algorithm>
  #include<functional>
  #include<iomanip>
  #include<cstdio>
  #include<cmath>
  #include<cstring>
  #include<cstdlib>
  #include<cassert>

解决方案

Two points to consider.

  1. getchar_unlocked is deprecated in Windows because it is thread unsafe version of getchar().

  2. Unless speed factor is too much necessary, try to avoid getchar_unlocked.

Now, as far as speed is concerned.

    getchar_unlocked > getchar

because there is no input stream lock check in getchar_unlocked which makes it unsafe.

    getchar > scanf

because getchar reads a single character of input which is char type whereas scanf can read most of the primitive types available in c.

    scanf > cin (>> operator)

because check this link

So, finally

getchar_unlocked > getchar > scanf > cin

这篇关于getchar_unlocked()VS scanf()VS cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆