gpt4 book ai didi

c++ - printf 与 std::cout

转载 作者:IT老高 更新时间:2023-10-28 21:34:49 28 4
gpt4 key购买 nike

Possible Duplicate:
Should I use printf in my C++ code?

如果我只想在屏幕上打印一个 string,我可以使用以下两种方式:

printf("abc");

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

在上述例子中,使用 printf 是否比 std::cout 有优势,反之亦然?

最佳答案

虽然 cout 是正确的 C++ 方式,但我相信有些人和公司(包括 Google )继续在 C++ 代码中使用 printf 因为它更容易使用 printf 进行格式化输出,而不是使用 cout

这是我发现的一个有趣的例子 here .

比较:

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

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

关于c++ - printf 与 std::cout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781819/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com