printf vs. std :: cout [英] printf vs. std::cout

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

问题描述


可能重复:

我应该在我的C ++代码中使用printf吗?

如果我只是想在屏幕上打印一个字符串,我可以使用这两种方法:

If I just want to print a string on screen, I can do that using those two ways:

printf("abc");

std::cout << "abc" << std::endl;

这种情况是,在上面的例子中,使用 printf over std :: cout ,反之亦然吗?

The case is, and in the examples shown above, is there an advantage of using printf over std::cout, or, vice versa?

推荐答案

虽然 cout 是正确的C ++方式,人和公司(包括 Google )继续使用 printf 在C ++代码中,因为使用 printf 比使用 cout

While cout is the proper C++ way, I believe that some people and companies (including Google) continue to use printf in C++ code because it is much easier to do formatted output with printf than with cout.

这是一个有趣的例子,我在这里找到了。

Here's an interesting example that I found here.

比较:

printf( "%-20s %-20s %5s\n" , "Name" , "Surname" , "Id" );

cout << setw( -20 ) << "Name" << setw( 20 ) << "Surname"  << setw( 5 ) << "Id" << endl;

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

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