endl和cout之后的行间距? [英] Line spacing after endl and cout?

查看:209
本文介绍了endl和cout之后的行间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在以下代码:

I noticed that in the following code:

    cout << "Please enter your number: ";
    cin >> Number;
    cout << "Is this spaced";

C ++的命令窗口中的输出自动在下一行中显示Is this spaced。它在 cin 行之后空格,而不需要使用流操纵器(为什么这被称为流操纵器?) endl 。如果我只是有代码:

The output in the command window for C++ automatically puts "Is this spaced" in the next line. It spaces whatever is after the cin line without needing to use the stream manipulator (why is this called a stream manipulator?) endl. Whereas if I just have the code:

    cout << "Please enter your number: ";
    cout << "Is this spaced";

它不会自动用Is this spaced相反,两条线连接起来。我很想知道为什么是这种情况,因为我一直认为你需要 endl 才能创建一个新的空间行。

It won't automatically space the line with "Is this spaced". Instead both lines are joined up. I am curious to know why this is the case because I always thought that you need endl in order to create a new line of space.

谢谢!

推荐答案

cout << "Please enter your number: ";
cin >> Number;
cout << "Is this spaced";

还有更多的东西要满足眼睛。 std :: cout std :: cin 默认为绑定流。这意味着 std :: cout 会自动刷新(即任何未决输出从程序中的任何缓冲区冲出到操作系统), std: :cin 被要求输入。这就是为什么你可以肯定看到请输入你的号码:之前,程序暂停等待你键入。当然,在大多数操作系统中,您可以在程序等待之前开始输入 - 它将回显它到终端,并记住它以后提供 std :: cin :这也是当你使用管道调用程序时会发生什么:

There's more to this than meets the eye. std::cout and std::cin are - by default - tied streams. That means that std::cout is automatically flushed (i.e. any pending output flushed from any buffers inside your program out to the operating system) whenever std::cin is asked for input. That's why you can be sure to see "Please enter your number: " before the program pauses to wait for you to type. Of course, in most Operating Systems you can start typing before the program's waiting - it will echo it to the terminal and remember it to provide to std::cin later: that's also what happens when you invoke a program with a pipeline such as:

echo "123" | the_program

the_program ,但是坐在那里 cin>> Number; 来尝试解析。在这种情况下,没有键盘输入终端程序回声,因此123 \\\
序列不回显到您的两行输出之间的屏幕 - 没有换行符\\\
您的输出

The input's available when the_program starts running, but sits there for cin >> Number; to attempt parsing. In this case though, there's no keyboard input for the terminal program to echo, and hence the "123\n" sequence isn't echoed to the screen between your two lines of output - without that newline "\n" your output will all appear on one line.

如果您希望在没有键盘输入的情况下从键盘读取光标到下一行,您最好使用ncurses或一些类似的库。库可以使用适合您的终端的转义序列(如果可用),以根据您的喜好重新定位光标。如果你有一个非常有限的终端支持范围(例如,只是xterm兼容,VT220或Windows命令shell),编码自己可能是实用的。通常也可以抑制键盘输入的打印,但是然后用户看不到自己键入数字。另一个选项是将终端设置为支持逐个字符输入读取的输入模式(一些终端默认为逐行,因此在返回被按下之前看不到字符) - 将其与在您的程序可以在输入时打印数字,但不打印换行符。

If you want to read from the keyboard without the keyboard input moving the cursor to the next line, you'd be best off using ncurses or some similar library. The libraries can use escape sequences appropriate to your terminal (if available) to reposition the cursor to your liking. It may be practical to code that up yourself if you have a very limited range of terminals to support (e.g. just xterm-compatible ones, VT220, or Windows command shells). It's also generally possible to suppress the printing of keyboard input, but then the user couldn't see themselves type the digits. Another option is to set the terminal to an input mode supporting character-by-character input reading (some terminals default to line-by-line so you can't see characters until return is pressed) - combining that with the suppressed echo above your program can print digits as they're typed, but not print the newline.

另外,最好用换行符结束程序的输出,因为一些调用环境否则显示最后一行。并且,它有点有争议,但IMHO最佳做法不使用 std :: endl 当你不需要刷新输出 - 只需使用 \

Separately, it's good practice to end your program's output with a newline, as some invocation environments won't show the final line otherwise. And, it's somewhat contentious but IMHO best practice not to use std::endl when you don't need to flush the output - just use \n and let the C++ iostream library buffer multiple lines and write them in efficiently sized chunks to the operating system.

假设你有这样的程序:

std::string h = "hello ";
std::string w = "world";

std::cout << h;
std::cout << w << '\n';

在某些阶段,程序需要告诉操作系统文本被打印,让它发送到一个shell / cmd提示符(可能会将其发送到屏幕上,并将其放在缓冲区滚动等),一个文件或任何。在事情的宏大计划中,程序告诉操作系统做这样的事情是缓慢的,所以如果程序作为一个整体将工作更快,如果它记住hello,添加世界和 \\\
(换行符),然后立即向操作系统发送hello world \\\
。程序内存储和数据连接称为缓冲,将数据从缓冲区写入操作系统的行为称为刷新。

At some stage, the program needs to tell the Operating System (Linux, Windows etc.) about the text to be printed, letting it send it to a shell/cmd prompt (which might send it on to the screen and put it in buffers for scrollbars etc.), a file or whatever. In the grand scheme of things, it's slow for the program to tell the operating system to do this kind of thing, so the program as a whole will work faster if it remembers "hello ", adds "world" and \n (a newline) to it, then sends "hello world\n" to the operating system all at once. That intra-program storage and concatenation of data is called buffering, and the act of writing data from the buffer to the Operating System is called flushing.

这篇关于endl和cout之后的行间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆