问题描述
是我的代码。我在问题中提供源,因为它不是那么大:
Here is my code. I am providing source in the question since it's not so big:
#include <stdio.h>
void scan(int* i)
{
int t=0;
char c;
bool negative=false;
c=getchar_unlocked();
while(c<'0'&&c>'9')
{
if(c=='-')
negative=true;
c=getchar_unlocked();
}
while(c>'0'&&c<'9')
{
t=(t<<3)+(t<<1)+c-'0';
c=getchar_unlocked();
}
if(negative)
t=~(t-1); //negative
*i=t;
}
int main(int argc, char const *argv[])
{
int i;
scan(&i);
return 0;
}
我知道这里定义的函数 scan
比 scanf
更快,对于编程比赛非常有用。但由于某些原因,这个代码不工作在windows和工作在linux。我该怎么做,使它在Windows上工作。我使用的 g ++
编译器 dev-c ++
。
I know that the function defined here as scan
is faster than scanf
and is very useful on programming competitions. But for some reason this code is not working on windows and is working on linux. What do I do to get it working on windows. I am using the g++
compiler of dev-c++
.
推荐答案
getchar_unlocked
不是C或C ++标准函数,因此它不会在Windows上工作。这是一个POSIX标准我想,但Windows编译器不支持所有POSIX函数。
getchar_unlocked
is not a C or C++ standard function and therefore it's no surprise that it doesn't work on Windows. It is a POSIX standard I think, but Windows compilers don't support all POSIX functions.
如果你替换 getchar_unlocked
getchar
,它会工作,虽然算法似乎不太对。
If you replaced getchar_unlocked
with getchar
, it would kind of work, although the algorithm doesn't seem quite right.
这样做与条件编译,像这样例如
You could do this with conditional compilation, like this for instance
#ifdef _WINDOWS
// no getchar_unlocked on Windows so just call getchar
inline int getchar_unlocked() { return getchar(); }
#endif
这篇关于getchar_unlocked在windows中未声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!