C++ Console::WriteLine() 与 cout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16557613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Console::WriteLine() vs. cout
提问by Ehren Patel
I've just started trying to teach myself C++ (I've been a C# programmer for about a year now) and I can't understand for the life of me what the difference is between Console::WriteLine("Hello World") and cout<<"Hello World", on a side note I'm not even really sure what cout and cin even are so any help with that would also be appreciated
我刚刚开始尝试自学 C++(我已经成为 C# 程序员大约一年了),我一生都无法理解 Console::WriteLine("Hello World") 之间的区别和 cout<<"Hello World",顺便说一句,我什至不确定 cout 和 cin 是什么,所以任何帮助也将不胜感激
回答by user93353
You are using C++/CLI and not just C++. C++/CLI is a Microsoft extension which allows you to write .NET code on Windows and allows you to use the .NET Library(CLR - Common Language Runtime).
您正在使用 C++/CLI 而不仅仅是 C++。C++/CLI 是一个 Microsoft 扩展,它允许您在 Windows 上编写 .NET 代码并允许您使用 .NET 库(CLR - 公共语言运行时)。
Console::WriteLine is a method from the .NET Library - http://msdn.microsoft.com/en-us/library/kxcchfk6.aspx
Console::WriteLine 是 .NET 库中的一种方法 - http://msdn.microsoft.com/en-us/library/kxcchfk6.aspx
When you are create a Project in Visual C++, it allows you to either create a C++ Project or a C++/CLI (CLR) project. The CLR Projects types are the ones where you can use .NET stuff. If you create a Win32 project or one of the other types, it's just C++.
在 Visual C++ 中创建项目时,它允许您创建 C++ 项目或 C++/CLI (CLR) 项目。CLR 项目类型是您可以使用 .NET 内容的类型。如果您创建 Win32 项目或其他类型之一,则它只是 C++。
If you are not creating projects & just compiling from the command line, then the /clr
option is the one to use for C++/CLI.
如果您不创建项目而只是从命令行编译,则该/clr
选项是用于 C++/CLI 的选项。
cout
& cin
are iostream
objects. The corresponding classes have operators <<
& >>
overloaded - hence you are able to do output with cout<<
& input with cin>>
.
cout
&cin
是iostream
对象。相应的类具有运算符<<
&>>
重载 - 因此您可以使用cout<<
& 输入与cin>>
.
This Q & Agives a better understanding of why the design used <<
& >>
.
这个问答可以更好地理解为什么设计使用<<
& >>
。
回答by NPE
The difference is that std::cout
is standard and is therefore available in any C++ compiler on any platform, whereas Console
is a Microsoft-specific extension.
不同之处在于它std::cout
是标准的,因此可以在任何平台上的任何 C++ 编译器中使用,而Console
它是 Microsoft 特定的扩展。