cin / cout与scanf / printf [英] cin/cout vs. scanf/printf

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

问题描述

您好,我有疑问(或2 :))。对于大数据而言,使用scanf / printf而不是cin / cout使得程序运行更快?
是真的吗?如果是,将scanf / printf与C ++代码混合是否合法?

程序应在1秒以内执行,提示是使用scanf / printf。

解决方案

Podrzut_z_Laweczki写道:


你好,我有问题(或2 :))。对于大数据而言,使用scanf / printf而不是cin / cout使得程序运行更快?
是真的吗?



所有一般化都是假的。


如果是的话,混合scanf / printf是否合法用C ++代码?



是的,这是合法的。 scanf / printf是C ++的一部分。


程序应该在1秒内执行,提示是使用

scanf / printf。



提示给谁?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

< br>

Podrzut_z_Laweczki写道:


您好,我有问题(或2 :))。对于大数据而言,使用scanf / printf而不是cin / cout使得程序运行更快?
是真的吗?



有时,但C风格的I / O功能也不是类型安全的,所以你的开发时间也可能会增加。尝试:


char c =''a'';

cout<< c;

printf("%s",c);


第三行体育未定义的行为,而第二行总是

是正确的。


如果是,将scanf / printf与C ++代码混合是否合法?



查看ios_base :: sync_with_stdio()(例如,
http://www.cplusplus.com/ref/iostrea...th_stdio.html)


程序应在1秒以内执行,提示是使用scanf / printf。



这取决于您的编译器和优化器,您的计算机,您的数据

来源,您的编程技能等等,不只是C vs. C ++

风格的I / O.


干杯! --M


2006年8月3日12:54:00 -0700,Podrzut_z_Laweczki < em ***** @ o2.pl>

在comp.lang.c中写道:++:


您好,我有问题(或2 :))。对于大数据而言,使用scanf / printf而不是cin / cout使得程序运行更快?
是真的吗?



是的,这是真的。不,这不是真的。 C和C ++标准都没有指定任何运算符或函数的绝对速度或相对速度。

C ++标准确实指定了某些操作的顺序,但是命令

单独没有定义速度。


如果是,将scanf / printf与C ++代码混合是否合法?



在C ++程序中使用< cstdiofunctions是合法的,你是否正确使用它们

。在C或

C ++中使用scanf()几乎不是一个好主意,因为正确使用它是非常困难和复杂的。


程序应在1秒以内执行,提示是使用scanf / printf。



什么程序?谁暗示了?当你使用cin和cout流时,程序需要多长时间?


首先编写一个运行正常且符合其所有程序的程序

要求。然后彻底测试它,特别是对于角落的情况。

然后,只有这样,如果它运行正常但速度太慢,你可以开始考虑如何让它更快。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang。 c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster? And if it is, is it legal to mix scanf/printf with C++ code?
Program should execute below 1 sec and the hint is to use scanf/printf.

解决方案

Podrzut_z_Laweczki wrote:

Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?

All generalizations are false.

And if it is, is it legal to mix scanf/printf with C++ code?

Yes, it is legal. scanf/printf are parts of C++.

Program should execute below 1 sec and the hint is to use
scanf/printf.

The hint to whom?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Podrzut_z_Laweczki wrote:

Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?

Sometimes, but the C-style I/O functions are also not typesafe, so your
development time may also increase. Try:

char c = ''a'';
cout << c;
printf( "%s", c );

The third line sports undefined behavior, while the second line always
gets it right.

And if it is, is it legal to mix scanf/printf with C++ code?

Look into ios_base::sync_with_stdio() (e.g., at
http://www.cplusplus.com/ref/iostrea...th_stdio.html).

Program should execute below 1 sec and the hint is to use scanf/printf.

It depends on your compiler and optimizer, your computer, your data
source, your programming skill, etc. etc. etc., not just C vs. C++
style I/O.

Cheers! --M


On 3 Aug 2006 12:54:00 -0700, "Podrzut_z_Laweczki" <em*****@o2.pl>
wrote in comp.lang.c++:

Hello, I have question (or 2 :)). Is that true that for a large data
using scanf/printf instead of cin/cout makes that the program runs
faster?

Yes, it''s true. No, it''s not true. Neither the C nor C++ standard
specify the absolutely or relative speed of any operator or function.
The C++ standard does specify the order of some operations, but order
alone does not define speed.

And if it is, is it legal to mix scanf/printf with C++ code?

It is legal to use <cstdiofunctions in a C++ program is you use them
correctly. It is rarely a good idea to use scanf() in either C or
C++, as it is very difficult and complex to use correctly.

Program should execute below 1 sec and the hint is to use scanf/printf.

What program? Who''s hint? How long does the program take when you
use the cin and cout streams?

First write a program that runs correctly and meets all its
requirements. Then test it thoroughly, especially for corner cases.
Then, and only then, if it runs correctly but too slowly, you can
start thinking about ways to make it faster.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


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

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