C++中的endl搭配cout和cin用法

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; // 如上所述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值