现在的位置: 首页 > 综合 > 正文

endl

2018年02月21日 ⁄ 综合 ⁄ 共 866字 ⁄ 字号 评论关闭
endl是C++标准库中的操控器(Manipulator),包含于<iostream>(<iostream>),命名空间(namespace):std。注意endl(为字母l)而非end1(数字1)。

endl英语意思是end of line,即一行输出结束,然后输出下一行。
endl与cout搭配使用,意思是输出结束。

按C++标准程序库中的描述其实现如下:
 template <class charT, class traits>
 std::basic_ostream<charT, traits>&
 std::endl (std::basic_ostream<charT, traits>& strm)
 {
     strm.put(strm.widen(\n'));
     strm.flush();
     return strm;
 }

可见endl只是一个模板函数。
其主要搭配iostream对象来使用,如cout、cerr等,其作用是:
 1.将换行符写入输出流,其中Unix/Linux换行符是\n,Windows中是\r\n,MAC中是\r;
 2.清空输出缓冲区。

c++中如果使用输入\输出符endl。
比如在语句 :
 cout<<"the id is"<<endl <<2;
 cout<<"the id is"<<i << endl;
那么意思是:
endl就相当于输出的时候回车。

第一句的输出是:
the id is
2
第二句的输出是:
the id is i
然后光标到了第二行。

额外的,还可以这样使用endl:
 std::endl(cout); // 等于 std::endl(std::cout);
 std::endl(cout << "this id is" << i); // 等于 std::endl(std::cout << "this id is" << i);
(注:这是由于Koenig looup法则)
其中第一句等同于:std::cout << std::endl; // 不能写成std::cout << endl;
第二句等于:std::cout << "this id is" << i << std::endl; // 如上所述

抱歉!评论已关闭.