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


当前回答

我不是程序员,但我曾是人为因素工程师。我觉得编程语言应该是容易学习、理解和使用的,这就要求它有一个简单而一致的语言结构。虽然所有的语言都是象征性的,因此,在其核心是任意的,但有惯例,遵循这些惯例使语言更容易学习和使用。

在c++和其他语言中,有大量的函数以函数(参数)的形式编写,这种语法最初是在前计算机时代用于数学中的函数关系。printf()遵循这种语法,如果c++的作者想要创建任何逻辑上不同的方法来读写文件,他们可以使用类似的语法简单地创建一个不同的函数。

在Python中,我们当然可以使用同样标准的对象进行打印。方法语法,即variablename。因为变量是对象,但在c++中它们不是。

我不喜欢cout语法,因为<<操作符不遵循任何规则。它是一个方法或函数,也就是说,它接受一个参数并对它做一些事情。然而,它被写成了一个数学比较运算符。从人为因素的角度来看,这是一个糟糕的方法。

其他回答

当然,你可以把“something”写得更好一点,以保持维护:

#include <iostream>
#include <cstdlib>

using namespace std;

class Something
{
    public:
        Something(int x, int y, int z) : a(x), b(y), c(z) { }
        int a;
        int b;
        int c;

        friend ostream& operator<<(ostream&, const Something&);

        void print() const { printf("%i, %i, %i\n", a, b, c); }
};

ostream& operator<<(ostream& o, const Something& s)
{
    o << s.a << ", " << s.b << ", " << s.c;
    return o;
}

int main(void)
{
    Something s(3, 2, 1);

    // Output with printf
    s.print(); // Simple as well, isn't it?

    // Output with cout
    cout << s << endl;

    return 0;
}

还有一个cout vs. printf的扩展测试,如果有人想做更多的测试,添加了一个'double'的测试(Visual Studio 2008,可执行文件的发布版本):

#include <stdio.h>
#include <iostream>
#include <ctime>

class TimedSection {
    char const *d_name;
    //timespec d_start;
    clock_t d_start;

    public:
        TimedSection(char const *name) :
            d_name(name)
        {
            //clock_gettime(CLOCK_REALTIME, &d_start);
            d_start = clock();
        }
        ~TimedSection() {
            clock_t end;
            //clock_gettime(CLOCK_REALTIME, &end);
            end = clock();
            double duration = /*1e3 * (end.tv_sec - d_start.tv_sec) +
                              1e-6 * (end.tv_nsec - d_start.tv_nsec);
                              */
                              (double) (end - d_start) / CLOCKS_PER_SEC;

            std::cerr << d_name << '\t' << std::fixed << duration * 1000.0 << " ms\n";
        }
};


int main() {
    const int iters = 1000000;
    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);
    }
    {
        TimedSection s("cout with formatted double (width & precision once)");
        std::cout << std::fixed << std::scientific << std::right << std::showpoint;
        std::cout.width(8);
        for (int i = 0; i < iters; ++i)
            std::cout << text << 8.315 << i << '\n';
    }
    {
        TimedSection s("cout with formatted double (width & precision on each call)");
        std::cout << std::fixed << std::scientific << std::right << std::showpoint;

        for (int i = 0; i < iters; ++i)
            { std::cout.width(8);
              std::cout.precision(3);
              std::cout << text << 8.315 << i << '\n';
            }
    }
    {
        TimedSection s("printf with formatted double");
        for (int i = 0; i < iters; ++i)
            printf("%8.3f%i\n", 8.315, i);
    }
}

结果是:

cout with only endl    6453.000000 ms
cout with only '\n'    125.000000 ms
printf with only '\n'    156.000000 ms
cout with string constant and endl    6937.000000 ms
cout with string constant and '\n'    1391.000000 ms
printf with string constant and '\n'    3391.000000 ms
cout with some stuff and endl    9672.000000 ms
cout with some stuff and '\n'    7296.000000 ms
printf with some stuff and '\n'    12235.000000 ms
cout with formatted double (width & precision once)    7906.000000 ms
cout with formatted double (width & precision on each call)    9141.000000 ms
printf with formatted double    3312.000000 ms
cout<< "Hello";
printf("%s", "Hello"); 

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

人们经常声称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);
    }
}

我不是程序员,但我曾是人为因素工程师。我觉得编程语言应该是容易学习、理解和使用的,这就要求它有一个简单而一致的语言结构。虽然所有的语言都是象征性的,因此,在其核心是任意的,但有惯例,遵循这些惯例使语言更容易学习和使用。

在c++和其他语言中,有大量的函数以函数(参数)的形式编写,这种语法最初是在前计算机时代用于数学中的函数关系。printf()遵循这种语法,如果c++的作者想要创建任何逻辑上不同的方法来读写文件,他们可以使用类似的语法简单地创建一个不同的函数。

在Python中,我们当然可以使用同样标准的对象进行打印。方法语法,即variablename。因为变量是对象,但在c++中它们不是。

我不喜欢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(),但如何写?)