【问题标题】:Why use endl when I can use a newline character? [duplicate]当我可以使用换行符时为什么要使用 endl? [复制]
【发布时间】:2011-11-11 14:18:27
【问题描述】:

当我可以使用\n 时,是否有理由将endlcout 一起使用?我的 C++ 书说要使用 endl,但我不明白为什么。 \n 没有像 endl 那样被广泛支持,还是我遗漏了什么?

【问题讨论】:

  • 明确地说,在绝大多数情况下,您应该使用endl。另见this answer + cmets。
  • 请记住,在 DOS/Windows 平台上,行尾是“\r\n”,而在某些(全部?)Mac 平台上也是“\r”。
  • @sylvainulg:这不是特别相关。打印到文本流的 '\n' 字符将自动转换为系统的行尾表示。就这一点而言,'\n'std::endl 之间没有区别(区别在于flush)。 (Mac平台,从OSX开始(我认为)是基于Unix的,并使用'\n'来标记行尾。)
  • 如果您确实需要尽可能频繁地刷新流(例如,在崩溃前查看调试输出),您可以使用std::cerr,它也不需要显式刷新。跨度>
  • @keith:感谢您的精确。这在我手头的文档中不是很清楚,而且我自己也不知道同花差异。

标签: c++ string-formatting iostream buffering


【解决方案1】:

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

cout << x << endl;

等价于

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

流可以使用内部缓冲区,当流被刷新时,该缓冲区会实际流式传输。在cout 的情况下,您可能不会注意到差异,因为它以某种方式与cin 同步(绑定),但是对于任意流,例如文件流,您会注意到一个差异例如多线程程序。

Here 就为何需要冲洗进行了有趣的讨论。

【讨论】:

  • 另外,endl 不是转换为正确的平台 EndLine 符号(Windows 上为“\r\n”,Linux 上为“\n”)吗?
  • C 标准规定'\n' 被透明地转换为适当的字符。所以,是的,因为endl 附加了'\n',这种情况正在发生。但它并不特定于endl
  • @Nawaz:避免混淆和过剩。支持说明
  • @Chad:是的——我们在这里讨论的 C++ 标准也是如此。
  • @Keith Thompson '\n' 被透明转换的事实在“C++”标准中没有明确说明;它是从 C 标准继承而来的。
【解决方案2】:

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

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

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

【讨论】:

  • 你的意思是'或任何其他缓冲输出流`。
  • 为什么 someFunction() 会在第一行之前执行?什么会导致这样的事情?
【解决方案3】:

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

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

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

看看这段代码(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;
}

编译此代码并查看输出。

回到主题...

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

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

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

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

结论:在 C++ 后面,C 的代码是有效的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 2011-02-15
    • 2013-01-06
    相关资源
    最近更新 更多