本文介绍了std :: cout可以完全替换printf吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好,我对c ++很新。很抱歉问这个看似愚蠢的问题。 可以 std :: cout 完全替换所有 printf 可以提供的? 特别是当 printf 可以在文本中轻松处理float中的小数位时。解决方案 我在这里,我同意我就是其中之一。我更喜欢使用printf(),因为我可以提供它的格式参数。像这样, #include < stdio.h > void main(){ const char * name = Afzaal Ahmad Zeeshan; int age = 20 ; char * message = 对所有人的爱,仇恨没有。; printf( 我的名字是%s,\ n我的年龄是%d,\\ \\ n%s,姓名,年龄,消息); } 现在在C ++领域也是如此, #include < iostream > void main(){ const char * name = Afzaal Ahmad Zeeshan; int age = 20 ; char * message = 对所有人的爱,仇恨没有。; std :: cout<< 我的名字是<<名称<< ,\ n我的年龄是<<年龄<< ,\ n<<消息<<的std :: ENDL; } 我不必说谁赢了,你知道谁。 :-)但是,仍然坚持C ++程序中的cout并且不考虑重用C函数和库。这正是Bjarne创建C ++的原因。 cout获胜的是你可以在输出流中传递几乎任何类型的东西,并且它将被接受。 参考文献: 请阅读这两篇文章的参考资料和手册,它们可以帮助您理解还有一些事情: 1. http://www.cplusplus.com/reference/ cstdio / printf / [ ^ ] 2. http://www.cplusplus.com/reference/iostream/cout/ [ ^ ] 注意:在C / C ++中,数组确实是一个指针,因此我使用 const char * name 而不是 char name [] 。我希望不会有任何冒犯。 : - ) 技术上是的,因为你可以继承自< iostream>和重载运算符<<所以完全取决于你: https://msdn.microsoft.com/en-us/library/ 1z2f6c2k.aspx [ ^ ] Hello, I'm quite new to c++. Sorry for asking this seemingly silly questions.Can std::cout fully replace all the printf could offer?Especially when printf can easily deal with decimal places in float within the text. 解决方案 Here I am, and I agree that I am one of them. I prefer using printf() because of the format arguments I can provide it with. Like this,#include <stdio.h>void main () { const char *name = "Afzaal Ahmad Zeeshan"; int age = 20; char *message = "Love for all, hatred for none."; printf("My name is %s, \nMy age is %d, \n%s", name, age, message);}Now the same in the realm of C++ would be something like this,#include <iostream>void main () { const char *name = "Afzaal Ahmad Zeeshan"; int age = 20; char *message = "Love for all, hatred for none."; std::cout << "My name is " << name << ",\nMy age is " << age << ", \n" << message << std::endl;}I don't have to say who won, you know who. :-) But still, stick to cout in C++ programs and do not consider re-using the C functions and libraries. That is exactly why Bjarne created C++. The thing where cout wins is that you can pass almost any type to it in the output stream, and it would be accepted.References:Please do read the references and manuals for these two, they may help you in understand a few more things:1. http://www.cplusplus.com/reference/cstdio/printf/[^]2. http://www.cplusplus.com/reference/iostream/cout/[^]Note: In C/C++ an array is indeed a pointer, so I used const char *name instead of char name[]. I hope no offence is raised on that. :-)Technically yes because you can inherit from <iostream> and overload operator << so it is entirely up to you: https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx[^] 这篇关于std :: cout可以完全替换printf吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 05:30