printf()和cout在c++中的区别是什么?
当前回答
更多的差异: "printf"返回一个整数值(等于打印的字符数),"cout"不返回任何东西
And.
Cout << "y = " << 7;不是原子的。
Printf ("%s = %d", "y", 7);是原子的。
Cout执行类型检查,printf不执行。
iostream中没有% d的等价物
其他回答
人们经常声称printf要快得多。这在很大程度上是一个神话。我刚刚测试了一下,结果如下:
cout with only endl 1461.310252 ms
cout with only '\n' 343.080217 ms
printf with only '\n' 90.295948 ms
cout with string constant and endl 1892.975381 ms
cout with string constant and '\n' 416.123446 ms
printf with string constant and '\n' 472.073070 ms
cout with some stuff and endl 3496.489748 ms
cout with some stuff and '\n' 2638.272046 ms
printf with some stuff and '\n' 2520.318314 ms
结论:如果你只想要换行,使用printf;否则,cout几乎一样快,甚至更快。更多细节可以在我的博客上找到。
需要澄清的是,我并不是说iostreams总是比printf更好;我只是想说,你应该根据真实的数据做出明智的决定,而不是基于一些常见的、误导性的假设而胡乱猜测。
更新:这里是我用于测试的完整代码。使用g++编译,没有任何其他选项(除了用于计时的-lrt)。
#include <stdio.h>
#include <iostream>
#include <ctime>
class TimedSection {
char const *d_name;
timespec d_start;
public:
TimedSection(char const *name) :
d_name(name)
{
clock_gettime(CLOCK_REALTIME, &d_start);
}
~TimedSection() {
timespec end;
clock_gettime(CLOCK_REALTIME, &end);
double duration = 1e3 * (end.tv_sec - d_start.tv_sec) +
1e-6 * (end.tv_nsec - d_start.tv_nsec);
std::cerr << d_name << '\t' << std::fixed << duration << " ms\n";
}
};
int main() {
const int iters = 10000000;
char const *text = "01234567890123456789";
{
TimedSection s("cout with only endl");
for (int i = 0; i < iters; ++i)
std::cout << std::endl;
}
{
TimedSection s("cout with only '\\n'");
for (int i = 0; i < iters; ++i)
std::cout << '\n';
}
{
TimedSection s("printf with only '\\n'");
for (int i = 0; i < iters; ++i)
printf("\n");
}
{
TimedSection s("cout with string constant and endl");
for (int i = 0; i < iters; ++i)
std::cout << "01234567890123456789" << std::endl;
}
{
TimedSection s("cout with string constant and '\\n'");
for (int i = 0; i < iters; ++i)
std::cout << "01234567890123456789\n";
}
{
TimedSection s("printf with string constant and '\\n'");
for (int i = 0; i < iters; ++i)
printf("01234567890123456789\n");
}
{
TimedSection s("cout with some stuff and endl");
for (int i = 0; i < iters; ++i)
std::cout << text << "01234567890123456789" << i << std::endl;
}
{
TimedSection s("cout with some stuff and '\\n'");
for (int i = 0; i < iters; ++i)
std::cout << text << "01234567890123456789" << i << '\n';
}
{
TimedSection s("printf with some stuff and '\\n'");
for (int i = 0; i < iters; ++i)
printf("%s01234567890123456789%i\n", text, i);
}
}
这里没有提到的两点我认为很重要:
1)如果你还没有使用STL, cout会携带很多包袱。它向目标文件中添加的代码是printf的两倍多。对于字符串也是如此,这也是我倾向于使用自己的字符串库的主要原因。
2) cout使用重载的<<操作符,我觉得这很不幸。如果还将<<运算符用于其预期目的(左移),则会增加混淆。我个人不喜欢为了与其预期用途无关的目的而重载操作符。
底线:如果我已经在使用STL,我将使用cout(和字符串)。否则,我倾向于避免它。
我想说,printf的可扩展性不足是不完全正确的: 在C语言中,它是真的。但是在C语言中,没有真正的类。 在c++中,可以重载强制转换操作符,因此,重载char*操作符并像这样使用printf:
Foo bar;
...;
printf("%s",bar);
可以是可能的,如果Foo重载好的操作符。或者你有一个好方法。简而言之,printf对我来说和cout一样具有可扩展性。
我可以看到c++流的技术参数(一般来说…不仅是cout.)是:
类型安全。(顺便说一下,如果我想打印一个'\n',我使用putchar('\n')…我不会用核弹杀死一只昆虫。) 更容易学。(不需要学习“复杂的”参数,只需使用<<和>>操作符) 本机使用std::string(对于printf有std::string::c_str(),但对于scanf?)
对于printf,我看到:
更容易,或至少更短(就书写的字符而言)复杂的格式。对我来说,可读性更强(我猜是品味问题)。 更好地控制函数所生成的内容(返回写入了多少字符并且有%n格式化程序:"Nothing printing . "参数必须是一个指向有符号整型的指针,其中存储到目前为止写入的字符数。”(from printf - c++ Reference) 更好的调试可能性。原因和上一个论点一样。
我个人倾向于使用printf(和scanf)函数,主要是因为我喜欢短行,而且我认为打印文本时的打字问题并不难避免。 我唯一对c风格函数感到遗憾的是std::string不受支持。在将其交给printf之前,我们必须通过一个char*(如果我们想读,则使用std::string::c_str(),但如何写?)
Printf是一个函数而cout是一个变量。
对我来说,真正的区别是让我选择'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中格式化数据(精度,合理性等)更容易。
推荐文章
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- printf()和puts()在C语言中的区别是什么?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- gcc在哪里查找C和c++头文件?