博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中的endl搭配cout和cin用法
阅读量:4303 次
发布时间:2019-05-27

本文共 828 字,大约阅读时间需要 2 分钟。

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.清空输出缓冲区。
在 中如果使用输入\输出符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; // 如上所述

转载地址:http://jilws.baihongyu.com/

你可能感兴趣的文章
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>