std :: cout可以完全替换printf吗? [英] Can std::cout fully replace printf?

查看:113
本文介绍了std :: cout可以完全替换printf吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我对c ++很新。很抱歉问这个看似愚蠢的问题。



可以 std :: cout 完全替换所有 printf 可以提供的?

特别是当 printf 可以在文本中轻松处理float中的小数位时。

解决方案

我会说:定义替换。您可以...吗? :-)



想一想:给定一些所需的文本输出,你总能用 std:cout ?当然你可以,所以,在这方面,你的问题的答案是是。



但就方便性而言,它将是一个替代品,代码或其他方面的可维护性?它会更好还是更糟(我写了引号,我的意思是)?最重要的部分是个人喜好和个人工作方式和习惯。有些人讨厌使用 std:cout ,而且我有点理解他们,但有些人可能会对这个问题的看法表现出相当不错的原因......



-SA


答案非常简单,在C ++领域,它已经取代了 print f函数。你编写这样的C ++程序,



  #include   <   iostream.h  >  

void main(){
std :: cout<< 来自cout的你好!;
}





不需要包含旧的C风格, stdio.h 并使用这些功能。所以在这种情况下,你应该考虑使用 cout



然而,在C领域,没有cout。因此,在这种情况下,它仍然是 printf 。正如谢尔盖已经说过的那样,

引用:

有些讨厌使用std:cout

我在这里,我同意我就是其中之一。我更喜欢使用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 [] 。我希望不会有任何冒犯。 : - )


printf()和scanf()是C函数,可以用C ++中的std :: cout和std :: cin代替。因此,可以将其视为是否使用< iostream>的问题。而不是传统的< cstdio>。这讨论了两种方法,并给出了使用< iostream>的一些令人信服的理由: https:// isocpp .org / wiki / faq / input-output#iostream-vs-stdio [ ^ ]



整个问题在这里详细讨论:http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c [ ^ ]



一种方法比另一种更好吗?你得到的答案可能会被历史和理性所染色。老C程序员非常熟悉< cstdio>而较新的程序员可能更喜欢采用规范的C ++方法并使用< iostream> ;.

Are Riff写道:

可以 std :: cout 完全替换所有 printf 可以提供的吗?

特别是当 printf 可以轻松处理十进制时浮动的地方

在文本中。



技术上是的,因为你可以继承自< 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.

解决方案

I would say: define "replacement". Could you? :-)

Think about it: given some desired text output, can you always implement it using std:cout? Of course you can, so, in this aspect, the answer to your question is "yes".

But it will be a "replacement" in terms of convenience, maintainability of code or other aspects? Will it be "better" or "worse" (I wrote quotation marks and I mean it)? Biggest part of it is a matter of personal preferences and personal working styles and habits. Some hate using std:cout, and I kinda understand them, but some may show really good reasons for the near-opposite look at the problem…

—SA


The answer is very much simple, in the realm of C++, it already has replaced the printf function. You write the C++ programs like this,

#include <iostream.h>

void main () {
   std::cout << "Hello from cout!";
}



There is no need to include the old C-style, stdio.h and use those functions. So in that case, you should consider using cout.

However, in the realm of C, there is no cout. Thus, in that case it is still printf. As Sergey already said,

Quote:

Some hate using std:cout

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. :-)


printf() and scanf() are the C functions which may be replaced by std::cout and std::cin in C++. Thus it can be seen as a question of whether to use <iostream> instead of the traditional <cstdio>. This discusses the 2 approaches and gives some compelling reasons for using <iostream>: https://isocpp.org/wiki/faq/input-output#iostream-vs-stdio[^]

The whole issue is discussed at length here: http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c[^]

Is one approach better than the other? The answer you get will probably be coloured as much by history as by reason. Old C programmers are very comfortable with <cstdio> while newer programmers may prefer to take the canonical C++ approach and use <iostream>.

Are Riff wrote:

Can std::cout fully replace all the printf could offer?
Especially when printf can easily deal with decimal places in float
within the text.


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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆