printf()和cout在c++中的区别是什么?
当前回答
cout<< "Hello";
printf("%s", "Hello");
两者都用于打印值。它们有完全不同的语法。c++两者都有,C 只有printf。
其他回答
cout<< "Hello";
printf("%s", "Hello");
两者都用于打印值。它们有完全不同的语法。c++两者都有,C 只有printf。
对我来说,真正的区别是让我选择'cout'而不是'printf':
1) <<操作符可以为我的类重载。
2) cout的输出流可以很容易地更改为一个文件: (:复制粘贴:)
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
cout << "This is sent to prompt" << endl;
ofstream file;
file.open ("test.txt");
streambuf* sbuf = cout.rdbuf();
cout.rdbuf(file.rdbuf());
cout << "This is sent to file" << endl;
cout.rdbuf(sbuf);
cout << "This is also sent to prompt" << endl;
return 0;
}
3)我发现cout更具可读性,特别是当我们有很多参数时。
cout的一个问题是格式化选项。在printf中格式化数据(精度,合理性等)更容易。
我不是程序员,但我曾是人为因素工程师。我觉得编程语言应该是容易学习、理解和使用的,这就要求它有一个简单而一致的语言结构。虽然所有的语言都是象征性的,因此,在其核心是任意的,但有惯例,遵循这些惯例使语言更容易学习和使用。
在c++和其他语言中,有大量的函数以函数(参数)的形式编写,这种语法最初是在前计算机时代用于数学中的函数关系。printf()遵循这种语法,如果c++的作者想要创建任何逻辑上不同的方法来读写文件,他们可以使用类似的语法简单地创建一个不同的函数。
在Python中,我们当然可以使用同样标准的对象进行打印。方法语法,即variablename。因为变量是对象,但在c++中它们不是。
我不喜欢cout语法,因为<<操作符不遵循任何规则。它是一个方法或函数,也就是说,它接受一个参数并对它做一些事情。然而,它被写成了一个数学比较运算符。从人为因素的角度来看,这是一个糟糕的方法。
来自c++常见问题解答:
[15.1] Why should I use <iostream> instead of the traditional <cstdio>? Increase type safety, reduce errors, allow extensibility, and provide inheritability. printf() is arguably not broken, and scanf() is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (using << and >>) is, relative to C (using printf() and scanf()): More type-safe: With <iostream>, the type of object being I/O'd is known statically by the compiler. In contrast, <cstdio> uses "%" fields to figure out the types dynamically. Less error prone: With <iostream>, there are no redundant "%" tokens that have to be consistent with the actual objects being I/O'd. Removing redundancy removes a class of errors. Extensible: The C++ <iostream> mechanism allows new user-defined types to be I/O'd without breaking existing code. Imagine the chaos if everyone was simultaneously adding new incompatible "%" fields to printf() and scanf()?! Inheritable: The C++ <iostream> mechanism is built from real classes such as std::ostream and std::istream. Unlike <cstdio>'s FILE*, these are real classes and hence inheritable. This means you can have other user-defined things that look and act like streams, yet that do whatever strange and wonderful things you want. You automatically get to use the zillions of lines of I/O code written by users you don't even know, and they don't need to know about your "extended stream" class.
另一方面,printf要快得多,因此在非常特定和有限的情况下,可以优先使用它而不是cout。总是先做侧写。(例如,参见http://programming-designs.com/2009/02/c-speed-test-part-2-printf-vs-cout/)
TL;DR:在相信在线随机评论之前,一定要自己做研究,考虑生成的机器代码的大小、性能、可读性和编码时间,包括这一条。
我不是专家。我碰巧听到两个同事在讨论如何避免在嵌入式系统中使用c++,因为会导致性能问题。有趣的是,我基于一个真实的项目任务做了一个基准测试。
在该任务中,我们必须向RAM写入一些配置。喜欢的东西:
咖啡=热 糖=没有 牛奶=乳房 mac = AA: BB: CC:弟弟:EE: FF
这是我的基准测试程序(是的,我知道OP询问printf(),而不是fprintf()。试着捕捉本质,顺便说一下,OP的链接指向fprintf()。)
C程序:
char coffee[10], sugar[10], milk[10];
unsigned char mac[6];
/* Initialize those things here. */
FILE * f = fopen("a.txt", "wt");
fprintf(f, "coffee=%s\nsugar=%s\nmilk=%s\nmac=%02X:%02X:%02X:%02X:%02X:%02X\n", coffee, sugar, milk, mac[0], mac[1],mac[2],mac[3],mac[4],mac[5]);
fclose(f);
c++程序:
//Everything else is identical except:
std::ofstream f("a.txt", std::ios::out);
f << "coffee=" << coffee << "\n";
f << "sugar=" << sugar << "\n";
f << "milk=" << milk << "\n";
f << "mac=" << (int)mac[0] << ":"
<< (int)mac[1] << ":"
<< (int)mac[2] << ":"
<< (int)mac[3] << ":"
<< (int)mac[4] << ":"
<< (int)mac[5] << endl;
f.close();
我尽了最大努力打磨它们,然后把它们都绕了10万次。以下是调查结果:
C程序:
real 0m 8.01s
user 0m 2.37s
sys 0m 5.58s
c++程序:
real 0m 6.07s
user 0m 3.18s
sys 0m 2.84s
目标文件大小:
C - 2,092 bytes
C++ - 3,272 bytes
结论:在我非常特定的平台上,使用非常特定的处理器,运行非常特定版本的Linux内核,运行一个非常特定版本的GCC编译的程序,以完成一个非常特定的任务,我会说c++方法更适合,因为它运行得更快,可读性更好。另一方面,C提供了小的内存占用,在我看来,这几乎没有什么意义,因为程序大小不是我们所关心的。
记住,YMMV。
推荐文章
- cplusplus.com给出的错误、误解或坏建议是什么?
- 找出质数最快的算法是什么?
- c++枚举类可以有方法吗?
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- 将析构函数设为私有有什么用?
- main()中的Return语句vs exit()
- 为什么c#不提供c++风格的'friend'关键字?
- 在函数的签名中添加关键字
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 为什么在标准容器中使用std::auto_ptr<>是错误的?
- 用比较double和0
- 保护可执行文件不受逆向工程的影响?
- 在c++中字符串前面的“L”是什么意思?
- 为什么std::map被实现为红黑树?
- 空括号的默认构造函数