C++ 当我可以使用换行符时为什么要使用 endl?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7324843/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:47:26  来源:igfitidea点击:

Why use endl when I can use a newline character?

c++string-formattingiostreambuffering

提问by Moshe

Is there a reason to use endlwith coutwhen I can just use \n? My C++ book says to use endl, but I don't see why. Is \nnot supported as widely as endl, or am I missing something?

有没有理由使用endlcout当我可以只使用\n?我的 C++ 书说要使用 endl,但我不明白为什么。是\n不支持广为endl,还是我失去了一些东西?

回答by Armen Tsirunyan

endlappends '\n'to the stream andcalls flush()on the stream. So

endl附加'\n'到流调用flush()流。所以

cout << x << endl;

is equivalent to

相当于

cout << x << '\n';
cout.flush();

A stream may use an internal buffer which gets actually streamed when the stream is flushed. In case of coutyou may not notice the difference since it's somehow synchronized (tied) with cin, but for an arbitrary stream, such as file stream, you'll notice a difference in a multithreaded program, for example.

流可以使用内部缓冲区,该缓冲区在刷新流时实际进行流式传输。如果cout您可能没有注意到差异,因为它以某种方式与 同步(绑定cin,但是对于任意流,例如文件流,您会注意到多线程程序中的差异,例如。

Here's an interesting discussion on why flushing may be necessary.

这里有一个有趣的讨论,讨论为什么可能需要冲洗。

回答by Paul Manta

endlis more than just an alias for the \ncharacter. When you send something to cout(or any other output stream), it does not process and output the data immediately. For example:

endl不仅仅是\n角色的别名。当您向cout(或任何其他输出流)发送内容时,它不会立即处理和输出数据。例如:

cout << "Hello, world!";
someFunction();

In the above example, there's is somechance that the function call will start to execute before the output is flushed. Using endlyou force the flush to take place before the second instruction is executed. You can also ensure that with the ostream::flushfunction.

在上面的例子中,有被一些机会,函数调用将开始执行前的输出被刷新。使用endl您强制在执行第二条指令之前进行刷新。您还可以确保具有该ostream::flush功能。

回答by user8175502

endl is a function not a keyword.

endl 是一个函数而不是一个关键字。

#include <iostream>
int main()
{
 std::cout<<"Hello World"<<std::endl;  //endl is a function without parenthesis.
 return 0;
}   

To understand the picture of endl you firstly need to understand about "Pointer to Functions" topic.

要了解 endl 的图片,您首先需要了解“函数指针”主题。

look at this code (in C)

看看这段代码(在 C 中)

#include <stdio.h>
int add(int, int);
int main()
{
   int (*p)(int, int); /*p is a pointer variable which can store the address    
   of a function whose return type is int and which can take 2 int.*/
   int x;

   p=add;                     //Here add is a function without parenthesis.

   x=p(90, 10); /*if G is a variable and Address of G is assigned to p then     
   *p=10 means 10 is assigned to that which p points to, means G=10                        
   similarly x=p(90, 10); this instruction simply says that p points to add    
   function then arguments of p becomes arguments of add i.e add(90, 10)   
   then add function is called and sum is computed.*/  

   printf("Sum is %d", x);
   return 0;
}
int add(int p, int q)
{
  int r;
  r=p+q;
  return r;
}

Compile this code and see the Output.

编译此代码并查看输出。

Back to topic...

回到主题...

 #include <iostream>
 //using namespace std; 
 int main()
 {
 std::cout<<"Hello World"<<std::endl;
 return 0;
 }

iostream file is included in this program because the prototype of cout object is present in iostream file and std is a namespace. It is used because defination(library files) of cout and endl is present in namespace std; Or you can also use "using namespace std" at top, so you don't have to write "std::coutn<<....." before each cout or endl.

iostream 文件包含在该程序中,因为 cout 对象的原型存在于 iostream 文件中,而 std 是一个命名空间。使用它是因为 cout 和 endl 的定义(库文件)存在于命名空间 std 中;或者您也可以在顶部使用“using namespace std”,这样您就不必在每个 cout 或 endl 之前写上“std::coutn<<.....”。

when you write endl without paranthesis then you give the address of function endl to cout then endl function is called and line is changed. The reason Behind this is

当您编写不带括号的 endl 时,您将函数 endl 的地址提供给 cout,然后调用 endl 函数并更改行。这背后的原因是

namespace endl
{
printf("\n");
}

Conclusion: Behind C++, code of C is working.

结论:在 C++ 背后,C 代码正在运行。