便携式线尾 [英] Portable end of line

查看:100
本文介绍了便携式线尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以根据所使用的操作系统自动使用正确的EOL字符?

is there any way to automatically use correct EOL character depending on the OS used?

我在想类似std::eol的事情吗?

我知道使用预处理器指令非常容易,但是很好奇它是否已经可用.

I know that it is very easy to use preprocessor directives but curious if that is already available.

我感兴趣的是,我通常在应用程序中有一些消息,以后将它们合并为一个字符串,并希望将它们与EOL分开.我知道我可以使用std::stringstream << endl,但是有时候代替常规的追加似乎有些过分.

What I am interested in is that I usually have some messages in my applications that I combine later into a single string and I want to have them separated with a EOL. I know that I could use std::stringstream << endl but it seems to be an overkill sometimes instead of a regular append.

推荐答案

std::endl被定义为除了向流中写入'\n'并将其刷新之外,不执行任何操作(第27.6.2.7节).刷新被定义为对stringstream不执行任何操作,因此您可以轻松地说出mystringstream << '\n'.操作系统上的标准库实现会正确转换\n,因此您不必担心.

std::endl is defined to do nothing besides write '\n' to the stream and flush it (§27.6.2.7). Flushing is defined to do nothing for a stringstream, so you're left with a pretty way of saying mystringstream << '\n'. The standard library implementation on your OS converts \n appropriately, so that's not your concern.

因此,endl已经是性能和可移植性的终极产品,如果您想有效地写入文件(而不是字符串流),则唯一需要的是<< '\n'.好吧,<< '\n'确实消除了对stringbuf::flush的无意义的虚拟调用.除非性能分析表明空函数调用需要花费时间,否则不要考虑它.

Thus endl is already the ultimate in performance and portability, and the only other thing you could want is << '\n' if you are trying to efficiently write to a file (not a stringstream). Well, << '\n' does also eliminate the pointless virtual call to stringbuf::flush. Unless profiling shows that empty function call to be taking time, don't think about it.

这篇关于便携式线尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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