博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
endl与\n的区别
阅读量:4315 次
发布时间:2019-06-06

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

看C++Primer的时候看到的,然后去百度了一下;

比较明白的解释:

1、区别在于:\n只代表换行的转义字符endl除了代表换行,还紧跟着清出缓冲槽2、接下来我们看一下具体内容的辨析:要明白\n和endl的区别,首先要明白一个概念:输出流。目前输出流的类型很多,有对文件作为目标输出的,有对显示屏(Console间接输出)进行输出的。也有输出到其他抽象结构的。cout的意思是console-output:控制台输出.但是它的机制还远没有我们想的那么简单,<
<后面跟着写什么就直接输出到屏幕什么?不是这样的。就拿cout>
<<"Hi,zw.\n"<
只要程序不蹦,还是没什么区别的,万一程序崩溃,\n可能会留在缓冲区里;

贴两段代码(及其汇编):

#include 
using namespace std;int main(){ cout<<"a"<
.file	"\316\264\303\374\303\3731.cpp".lcomm __ZStL8__ioinit,1,1	.def	___main;	.scl	2;	.type	32;	.endef	.section .rdata,"dr"LC0:	.ascii "a\0"	.text	.globl	_main	.def	_main;	.scl	2;	.type	32;	.endef_main:LFB971:	.cfi_startproc	leal	4(%esp), %ecx	.cfi_def_cfa 1, 0	andl	$-16, %esp	pushl	-4(%ecx)	pushl	%ebp	.cfi_escape 0x10,0x5,0x2,0x75,0	movl	%esp, %ebp	pushl	%ecx	.cfi_escape 0xf,0x3,0x75,0x7c,0x6	subl	$20, %esp	call	___main	movl	$LC0, 4(%esp)	movl	$__ZSt4cout, (%esp)	call	__ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc	movl	$__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, (%esp)	movl	%eax, %ecx	call	__ZNSolsEPFRSoS_E	subl	$4, %esp	movl	$0, %eax	movl	-4(%ebp), %ecx	.cfi_def_cfa 1, 0	leave	.cfi_restore 5	leal	-4(%ecx), %esp	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE971:	.def	___tcf_0;	.scl	3;	.type	32;	.endef___tcf_0:LFB981:	.cfi_startproc	pushl	%ebp	.cfi_def_cfa_offset 8	.cfi_offset 5, -8	movl	%esp, %ebp	.cfi_def_cfa_register 5	subl	$8, %esp	movl	$__ZStL8__ioinit, %ecx	call	__ZNSt8ios_base4InitD1Ev	leave	.cfi_restore 5	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE981:	.def	__Z41__static_initialization_and_destruction_0ii;	.scl	3;	.type	32;	.endef__Z41__static_initialization_and_destruction_0ii:LFB980:	.cfi_startproc	pushl	%ebp	.cfi_def_cfa_offset 8	.cfi_offset 5, -8	movl	%esp, %ebp	.cfi_def_cfa_register 5	subl	$24, %esp	cmpl	$1, 8(%ebp)	jne	L4	cmpl	$65535, 12(%ebp)	jne	L4	movl	$__ZStL8__ioinit, %ecx	call	__ZNSt8ios_base4InitC1Ev	movl	$___tcf_0, (%esp)	call	_atexitL4:	leave	.cfi_restore 5	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE980:	.def	__GLOBAL__sub_I_main;	.scl	3;	.type	32;	.endef__GLOBAL__sub_I_main:LFB982:	.cfi_startproc	pushl	%ebp	.cfi_def_cfa_offset 8	.cfi_offset 5, -8	movl	%esp, %ebp	.cfi_def_cfa_register 5	subl	$24, %esp	movl	$65535, 4(%esp)	movl	$1, (%esp)	call	__Z41__static_initialization_and_destruction_0ii	leave	.cfi_restore 5	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE982:	.section	.ctors,"w"	.align 4	.long	__GLOBAL__sub_I_main	.ident	"GCC: (GNU) 4.8.1"	.def	__ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;	.scl	2;	.type	32;	.endef	.def	__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_;	.scl	2;	.type	32;	.endef	.def	__ZNSolsEPFRSoS_E;	.scl	2;	.type	32;	.endef	.def	__ZNSt8ios_base4InitD1Ev;	.scl	2;	.type	32;	.endef	.def	__ZNSt8ios_base4InitC1Ev;	.scl	2;	.type	32;	.endef	.def	_atexit;	.scl	2;	.type	32;	.endef
#include 
using namespace std; int main(){ cout<<"a"<<"\n"; return 0; }
.file	"\316\264\303\374\303\3732.cpp".lcomm __ZStL8__ioinit,1,1	.def	___main;	.scl	2;	.type	32;	.endef	.section .rdata,"dr"LC0:	.ascii "a\0"LC1:	.ascii "\12\0"	.text	.globl	_main	.def	_main;	.scl	2;	.type	32;	.endef_main:LFB971:	.cfi_startproc	pushl	%ebp	.cfi_def_cfa_offset 8	.cfi_offset 5, -8	movl	%esp, %ebp	.cfi_def_cfa_register 5	andl	$-16, %esp	subl	$16, %esp	call	___main	movl	$LC0, 4(%esp)	movl	$__ZSt4cout, (%esp)	call	__ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc	movl	$LC1, 4(%esp)	movl	%eax, (%esp)	call	__ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc	movl	$0, %eax	leave	.cfi_restore 5	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE971:	.def	___tcf_0;	.scl	3;	.type	32;	.endef___tcf_0:LFB976:	.cfi_startproc	pushl	%ebp	.cfi_def_cfa_offset 8	.cfi_offset 5, -8	movl	%esp, %ebp	.cfi_def_cfa_register 5	subl	$8, %esp	movl	$__ZStL8__ioinit, %ecx	call	__ZNSt8ios_base4InitD1Ev	leave	.cfi_restore 5	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE976:	.def	__Z41__static_initialization_and_destruction_0ii;	.scl	3;	.type	32;	.endef__Z41__static_initialization_and_destruction_0ii:LFB975:	.cfi_startproc	pushl	%ebp	.cfi_def_cfa_offset 8	.cfi_offset 5, -8	movl	%esp, %ebp	.cfi_def_cfa_register 5	subl	$24, %esp	cmpl	$1, 8(%ebp)	jne	L4	cmpl	$65535, 12(%ebp)	jne	L4	movl	$__ZStL8__ioinit, %ecx	call	__ZNSt8ios_base4InitC1Ev	movl	$___tcf_0, (%esp)	call	_atexitL4:	leave	.cfi_restore 5	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE975:	.def	__GLOBAL__sub_I_main;	.scl	3;	.type	32;	.endef__GLOBAL__sub_I_main:LFB977:	.cfi_startproc	pushl	%ebp	.cfi_def_cfa_offset 8	.cfi_offset 5, -8	movl	%esp, %ebp	.cfi_def_cfa_register 5	subl	$24, %esp	movl	$65535, 4(%esp)	movl	$1, (%esp)	call	__Z41__static_initialization_and_destruction_0ii	leave	.cfi_restore 5	.cfi_def_cfa 4, 4	ret	.cfi_endprocLFE977:	.section	.ctors,"w"	.align 4	.long	__GLOBAL__sub_I_main	.ident	"GCC: (GNU) 4.8.1"	.def	__ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;	.scl	2;	.type	32;	.endef	.def	__ZNSt8ios_base4InitD1Ev;	.scl	2;	.type	32;	.endef	.def	__ZNSt8ios_base4InitC1Ev;	.scl	2;	.type	32;	.endef	.def	_atexit;	.scl	2;	.type	32;	.endef

本弱汇编还在学= = 所以很多看不懂=- =~

等能看懂了,再回来研究研究

转载于:https://www.cnblogs.com/XqwKen/p/4564318.html

你可能感兴趣的文章
IT职场人生系列之十五:语言与技术II
查看>>
如何在FreePBX ISO 中文版本安装讯时网关,潮流16FXS 网关和潮流话机
查看>>
基于Wolfpack开发业务监控系统
查看>>
通过Jexus 部署 dotnetcore版本MusicStore 示例程序
查看>>
程序员最常见的谎话和我自己的理解
查看>>
mine 数据
查看>>
poj2728 Desert King
查看>>
三个和尚的故事 与 项目机构管理
查看>>
Excel 日期转换
查看>>
js中的NaN、Infinity、null和undefined
查看>>
Runtime
查看>>
struts2 if标签示例
查看>>
Animate CSS
查看>>
.NET安全审核检查表
查看>>
application.properties数据库敏感信息加密这么简单?
查看>>
Language Codes: ISO 639, Microsoft and Macintosh
查看>>
centos6 x64安装elasticsearch5.5.2启动报错
查看>>
公司拷贝回家的工程用sts导入clean package报错java.lang.NoClassDefFoundError
查看>>
Aborting a running program
查看>>
手机验证码测试点
查看>>