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

Mei*_*eir 30 c++ split

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

通常我会在有逗号或其他合适的中断的地方分隔.

如何将此行分为2行?

cout<<"Error:This is a really long error message that exceeds the maximum permitted length.\n";
Run Code Online (Sandbox Code Playgroud)

如果我只是在中间某处按下输入它就不起作用了.

Tho*_*mas 43

两种选择:

cout << "Error:This is a really long "
 << "error message that exceeds "
 << "the maximum permitted length.\n";
Run Code Online (Sandbox Code Playgroud)

要么:

cout << "Error:This is a really long "
    "error message that exceeds "
    "the maximum permitted length.\n";
Run Code Online (Sandbox Code Playgroud)

第二个更有效率.

  • 对第二种情况的解释很方便:ANSI C允许连接字符串文字,如"foo""bar""baz"将与"foobarbaz"相同 (12认同)
  • 由编译器连接,而不是在运行时. (9认同)

Agn*_*ian 23

cout<<"Error:This is a really long error "
"message that exceeds the maximum permitted length.\n";
Run Code Online (Sandbox Code Playgroud)

要么

cout<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";
Run Code Online (Sandbox Code Playgroud)

要么

c\
o\
u\
t<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";
Run Code Online (Sandbox Code Playgroud)

  • 强烈建议避免在行尾使用反斜杠的变体.你没有机会通过反斜杠换行组合来展示多行分隔的评论开始和结束符号.:d (6认同)

laa*_*lto 8

cout << "Error:This is a really long error message "
    "that does not exceed the maximum permitted length.\n";
Run Code Online (Sandbox Code Playgroud)


cor*_*ttk 7

只是我的两个价值...

我不会包装那行代码.我会把它留作一根长长的绳子.

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

这些限制今天不适用.我的文本编辑器显示了150列的52行10pt courier new.我的工作监视器会显示400到65(我从未测试过).我多年来没有打印过一行源代码......而我最后一次这样做的原因是,当我的笔记本电脑出现在家里​​时,我可以在回家的路上看到它.

现代LANGUES有多少不是"旧式"的语言更详细的...这是很好的.如果你在Pascal中调用了BeanContextServicesSupport.BCSSServiceProvider,你的老板会告诉你坐在角落里.Pascal标识符,其中只有8个字符!

那么为什么坚持这个过时的(对我来说)讨厌的惯例呢?它几乎没有实际意义.

所以...我将"代码行"换成132个字符.我根本不打算包装"文本行".

另见:两匹马驴的宽度!

干杯.基思.

  • 80个字符的限制具有历史根源,但主要是因为人眼更好地跟踪短线. (15认同)