这个C ++代码究竟做了什么?你以前见过吗? [英] what exactly does this C++ code do ?have you ever seen in before ?

查看:93
本文介绍了这个C ++代码究竟做了什么?你以前见过吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++程序中我发现了这个

in a c++ program I found this

#ifdef _WIN32
inline int getchar_unlocked() { return getchar(); }
#endif
inline int ReadInt()
{
 int c=getchar_unlocked();
 int out=0;
 while(c=='\n' || c==' ')
 c=getchar_unlocked();
 while(c!='\n' && c!=' ' && c!=-1)
 {
 out*=10;
 out+=(c-'0');
 c=getchar_unlocked();
 }
 return out;
}



与:


in conjuction with :

{ freopen("filename.ext","rt",stdin);



每次ReadInt()被称为filename.txt中的整数被返回

例如,如果文件名是

141

5159 55 5818

4

然后5 ReadInt()将给出数字141,5159,55,5818和4

我的问题是这个人到底在做什么?

他是否使用标准输入流作为文件流?然后再次... getchar()从控制台返回一个char ...

这个


every time ReadInt() is called an integer from the filename.txt is returned
eg if filename is
141
5159 55 5818
4
then 5 ReadInt()'s will give the numbers 141,5159,55,5818 and 4
my question is what the hell is this guy doing ?
is he using the standard input stream as a file stream ? and then again...getchar() returns a char from the console...
and where does this

#ifdef _WIN32

这段代码帮助?(不是characted_unlocked()行......'if'steatement)...这个程序是在linux上编译和运行的,为什么会有人这样做?



另外:如何在不使用外部库(如内存映射文件方法)的情况下尽可能快地读取大(8-20 mb)文件(大多数文件只包含整数)?

fscanf比ifstream但上面的代码甚至比fscanf更快



感谢你的帮助:)

piece of code help ?( not the characted_unlocked() line..the 'if' steatement) ...this program is compiled and run on linux so why would someone do this ?

also : how can I read big (8-20 mb) files -most files contain integers only-as fast as possible without using external libraries such as the memory mapped file method ?
fscanf is way faster than ifstream but the code above is faster even than fscanf

thanks for your help :)

推荐答案

它是一种从标准输入流中读取整数的老式(C风格)方式。对 freopen 的调用将该文件重定向到 stdin ,以便可以通过调用来读取它的getchar 。更好的方法是使用 fscanf 或其中一个STL ifstream 类。顶部的 #ifdef _WIN32 没有被相应的 #endif 关闭,所以任何人都猜到它为什么存在。
It is a rather old fashioned (C-style) way of reading integers from the standard input stream. The call to freopen redirects that file to stdin so it can be read with the calls to getchar. A much better way of doing it would be to use fscanf or one of the STL ifstream classes. The #ifdef _WIN32 at the top is not closed by a corresponding #endif so it's anyone's guess why it is there.


该人正在使用输入流重定向,以便使用 getchar 来执行 fgetc [ ^ ]任务,即从文件中读取一个字符。

ReadInt 函数中,空格和换行符是跳过,然后读取数字并用于构建结果整数,直到找到空白,换行或文件结尾。



构建的关键语句读取数字的整数是:

The guy is using input stream redirection in order to use getchar to perform fgetc[^] task, that is reading a character at time from a file.
In the ReadInt function, blanks and newlines are skipped, then digits are then read and used to build the resulting integer, until a blank, a newline or the end of file is found.

The key statements for building the integer from read digits are:
int out=0;
 //..
while(/*...*/)
{
  out *= 10;
  out+=(c-'0');
  //..





那是 out 初始化为零,然后在每一步它首先乘以10然后加上读数位值(例如'5' - '0'== 5 )。

假设您正在从文件中读取55号。

  • out = 0 在读取任何数字之前
  • out 是在执行<$ c后执行乘法 10
  • out = 5 后仍为0 $ c> out + = c - '0',第一次
  • out = 50 ,在执行<$ c后$ c> out * = 10 下一步
  • out = 55 ,执行 out +后= c - '0'再次
  • 最后检测到空白并且函数返回 55


  • That is out is initalised with zero, then at each step it is first multiplied by 10 and then added with the read digit value (e.g. '5' - '0' == 5).
    Suppose you are reading the 55 number from the file.

    • out=0 before reading any digit
    • out is still 0 after performing the multiplication by 10
    • out=5, after performing out+=c - '0', the first time
    • out=50, after perorming out*=10 at next step
    • out=55, after performing out+=c - '0' again
    • finally a blank is detected and the function return 55

    • 这篇关于这个C ++代码究竟做了什么?你以前见过吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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