当前位置首页 > 百科> 正文

endl

2019-08-14 12:07:15 百科
endl

endl

endl是C++标準库中的操控器(Manipulator),包含于<iostream>,命名空间(namespace):std,其主要搭配iostream对象来使用,如cout、cerr等等。

基本介绍

  • 外文名:endl
  • 性质:C++程式语言
  • 意思:一行输出结束,然后输出下一行
  • 全称:end of line

介绍

endl英语意思是end of line,即一行输出结束,然后输出下一行。
endl与cout搭配使用,意思是输出结束。
按C++标準程式库中的描述其实现如下:
template <class charT, class traits>basic_ostream<charT,traits>& endl (basic_ostream<charT,traits>& os);
可见endl只是一个函式模板。

作用

1.将换行符写入输出流,并将与设备关联的缓冲区的内容刷到设备中,保证目前为止程式所暂存的所有输出都真正写入输出流。其中Unix/Linux换行符是\n,Windows中是\r\n,MAC中是\r;
2.清空输出缓冲区。

示例

例一

在语句 :
cout<<"the id is"<<endl <<2;cout<<"the id is"<<2 << endl;
中,endl就相当于输出的时候回车。
第一句的输出是:
the id is2
第二句的输出是:
the id is 2
然后游标到了第二行。

例二

额外的,还可以这样使用endl:
std::endl(cout); // 等于 std::endl(std::cout);std::endl(cout << "this id is" << 2); // 等于 std::endl(std::cout << "this id is" << 2);
(注:这是由于Koenig looup法则)
其中第一句等同于:
std::cout << std::endl; // 不能写成std::cout << endl;
第二句等于:
std::cout << "this id is" << 2 << std::endl; // 如上所述
声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:baisebaisebaise@yeah.net