cin和scanf的效率差异

您所在的位置:网站首页 cin和vain哪个严重 cin和scanf的效率差异

cin和scanf的效率差异

2024-07-15 09:01| 来源: 网络整理| 查看: 265

The global objects std::cin and std::wcin control input from a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C input stream stdin.

These objects are guaranteed to be constructed before the first constructor of a static object is called and they are guaranteed to outlive the last destructor of a static object, so that it is always possible to read from std::cin in user code.

Unless sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted input.

Once std::cin is constructed, std::cin.tie() returns &std::cout, and likewise, std::wcin.tie() returns &std::wcout. This means that any formatted input operation on std::cin forces a call to std::cout.flush() if any characters are pending for output.

一个比较合理的原因是:C++中cin为了和scanf保持同步, 这样大家可以混用两种方法,不至于文件指针乱码导致发生错误,因此cin会牺牲掉一点效率。如果想解除这种同步,可以使用语句cin.sync_with_stdio(false)。这样cin和scanf的效率基本相差不大。有人做了cin和scanf具体的读入时间测试,可以参见https://www.byvoid.com/blog/fast-readfile


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3