printf()和cout在c++中的区别是什么?


当前回答

我想指出的是,如果你想在c++中使用线程,如果你使用cout,你可以得到一些有趣的结果。

考虑下面的代码:

#include <string>
#include <iostream>
#include <thread>

using namespace std;

void task(int taskNum, string msg) {
    for (int i = 0; i < 5; ++i) {
        cout << "#" << taskNum << ": " << msg << endl;
    }
}

int main() {
    thread t1(task, 1, "AAA");
    thread t2(task, 2, "BBB");
    t1.join();
    t2.join();
    return 0;
}

// g++ ./thread.cpp -o thread.out -ansi -pedantic -pthread -std=c++0x

现在,输出都被打乱了。它也可以产生不同的结果,试着执行几次:

##12::  ABABAB

##12::  ABABAB

##12::  ABABAB

##12::  ABABAB

##12::  ABABAB

你可以使用printf,也可以使用互斥。

#1: AAA
#2: BBB
#1: AAA
#2: BBB
#1: AAA
#2: BBB
#1: AAA
#2: BBB
#1: AAA
#2: BBB

玩得开心!

其他回答

我想说,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(),但如何写?)

cout<< "Hello";
printf("%s", "Hello"); 

两者都用于打印值。它们有完全不同的语法。c++两者都有,C 只有printf。

来自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/)

我引用一下:

在高层术语中,主要的区别是类型安全(cstdio 没有它),性能(大多数iostreams实现都有 比cstdio慢)和可扩展性(iostreams允许 自定义输出目标和用户定义类型的无缝输出)。

这里没有提到的两点我认为很重要:

1)如果你还没有使用STL, cout会携带很多包袱。它向目标文件中添加的代码是printf的两倍多。对于字符串也是如此,这也是我倾向于使用自己的字符串库的主要原因。

2) cout使用重载的<<操作符,我觉得这很不幸。如果还将<<运算符用于其预期目的(左移),则会增加混淆。我个人不喜欢为了与其预期用途无关的目的而重载操作符。

底线:如果我已经在使用STL,我将使用cout(和字符串)。否则,我倾向于避免它。