如何在 C++ 中拆分长代码行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/969394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:10:19  来源:igfitidea点击:

How to split long lines of code in c++?

c++split

提问by Meir

I need to make sure none of the lines in my code exceeds a a certain length.

我需要确保我的代码中的任何行都没有超过一定的长度。

Normally I separate lines where there's a comma or another suitable break.

通常我会在有逗号或其他合适的中断处分隔行。

How can I separate this line into 2?

我怎样才能把这条线分成2条?

cout<<"Error:This is a really long error message that exceeds the maximum permitted length.\n";

If I just press enter somewhere in the middle it doesn't work.

如果我只是在中间的某个地方按回车,它就不起作用。

回答by Thomas

Two options:

两种选择:

cout << "Error:This is a really long "
 << "error message that exceeds "
 << "the maximum permitted length.\n";

Or:

或者:

cout << "Error:This is a really long "
    "error message that exceeds "
    "the maximum permitted length.\n";

The second one is more efficient.

第二种效率更高。

回答by Agnel Kurian

cout<<"Error:This is a really long error "
"message that exceeds the maximum permitted length.\n";

or

或者

cout<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";

or

或者

c\
o\
u\
t<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";

回答by laalto

cout << "Error:This is a really long error message "
    "that does not exceed the maximum permitted length.\n";

回答by corlettk

Just my 2 bobs worth...

只是我的 2 个鲍勃值得...

I wouldn't wrap that line of code. I'd leave it as one big long string.

我不会包装那行代码。我会把它留成一根长长的大绳子。

The 80 character convention was based on the limitations of the machinery of the day. Terminals where typically 80 by 32 characters. Cheap dot-matrix printers + continious-sheet paper was 80 characters. Only the rich people could afford a 132 character setup. And guess what... those who could afford it wrapped code at 132 characters, which dramatically decreases the number of lines which must be wrapped, and produces "cleaner" source code.

80 个字符的约定是基于当时机器的局限性。终端通常为 80 x 32 个字符。便宜的点阵打印机 + 连续纸是 80 个字符。只有富人才能负担得起 132 个字符的设置。猜猜看……那些负担得起的人以 132 个字符包装代码,这大大减少了必须包装的行数,并生成“更干净”的源代码。

Those constraints don't apply today. My text editor displays 150 columns by 52 lines of 10pt courier new. My work monitors would display something like 400 by 65 (I've never tested it). I haven't printed a single line of source code in years... and the last time I did so was so that I could read it one the bus on the way home, when my laptop was on the fritz.

这些限制今天不再适用。我的文本编辑器显示 150 列 x 52 行的 10pt courier new。我的工作监视器会显示 400 x 65(我从未测试过)。我已经好多年没有打印过一行源代码了……上次我这样做是为了在回家的路上我可以在公共汽车上阅读它,当时我的笔记本电脑已经坏了。

Modern langues are muchmore verbose than "older style" languages... and this is good. If you called anything a BeanContextServicesSupport.BCSSServiceProvider in Pascal your boss would have told you to go sit in the corner. Pascal identifiers where only significant to 8 characters!

现代语言比“旧式”语言冗长得多……这很好。如果你在 Pascal 中调用了 BeanContextServicesSupport.BCSSServiceProvider,你的老板会告诉你坐在角落里。帕斯卡标识符只有 8 个字符有效!

So why persist with this outmoded and (to me) annoying convention? It makes very little practical sense.

那么为什么要坚持这种过时的(对我来说)令人讨厌的约定呢?它没有什么实际意义。

So... I wrap "code lines" at 132 characters. I don't bother to wrap "text lines" at all.

所以...我用 132 个字符包装“代码行”。我根本不想包装“文本行”。

See also: The width of two horses arses!

另见:两匹马屁股的宽度!

Cheers. Keith.

干杯。基思。

回答by UBpine Inc

This will work on all the C++, weather it's VS, or on Linux

这将适用于所有 C++、天气它是 VS 或在 Linux 上

cout<<"Error:This is a really long error message that \
    exceeds the maximum permitted length.\n";