-
endl
鎖定
- 外文名
- endl
- 性 質
- C++程序語言
- 意 思
- 一行輸出結束,然後輸出下一行
- 全 稱
- end of line
endl基本信息
endl英語意思是end of line,即一行輸出結束,然後輸出下一行。
endl與cout搭配使用,意思是輸出結束。
按C++標準程序庫中的描述其實現如下:
template <class charT, class traits> basic_ostream<charT,traits>& endl (basic_ostream<charT,traits>& os);
可見endl只是一個函數模板。
endl作用
1.將換行符寫入輸出流,並將與設備關聯的緩衝區的內容刷到設備中,保證目前為止程序所暫存的所有輸出都真正寫入輸出流。
[1]
其中Unix/Linux換行符是\n,Windows中是\r\n,MAC中是\r;
2.清空輸出緩衝區。
endl示例
endl例一
在語句 :
cout<<"the id is"<<endl <<2; cout<<"the id is"<<2 << endl;
中,endl就相當於輸出的時候回車。
第一句的輸出是:
the id is 2
第二句的輸出是:
the id is 2
然後光標到了第二行。
endl例二
額外的,還可以這樣使用endl:
std::endl(cout); // 等於 std::endl(std::cout); std::endl(cout << "this id is" << 2); // 等於 std::endl(std::cout << "this id is" << 2);
(注:這是由於Koenig lookup法則)
其中第一句等同於:
std::cout << std::endl; // 不能寫成std::cout << endl;
第二句等於:
std::cout << "this id is" << 2 << std::endl;//如上所述