将 C++ 字符串拆分为多行(代码语法,不解析)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3859157/
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-28 13:54:51  来源:igfitidea点击:

Splitting C++ Strings Onto Multiple Lines (Code Syntax, Not Parsing)

c++stringsyntaxcoding-stylereadability

提问by Jason R. Mick

Not to be confused with how to split a string parsing wise, e.g.:
Split a string in C++?

不要与如何明智地拆分字符串混淆,例如:
在 C++ 中拆分字符串?

I am a bit confused as to how to split a string onto multiple lines in c++.

我对如何在 C++ 中将字符串拆分为多行感到有些困惑。

This sounds like a simple question, but take the following example:

这听起来像一个简单的问题,但请看下面的例子:

#include <iostream>
#include <string>
main() {
  //Gives error
  std::string my_val ="Hello world, this is an overly long string to have" +
    " on just one line";
  std::cout << "My Val is : " << my_val << std::endl;

  //Gives error
  std::string my_val ="Hello world, this is an overly long string to have" &
    " on just one line";  
  std::cout << "My Val is : " << my_val << std::endl;
}

I realize that I could use the std::stringappend()method, but I was wondering if there was any shorter/more elegant (e.g. more pythonlike, though obviously triple quotes etc. aren't supported in c++) way to break strings in c++ onto multiple lines for sake of readability.

我意识到我可以使用该std::stringappend()方法,但我想知道是否有任何更短/更优雅(例如更像 python,但显然三引号等在 c++ 中不受支持)的方法来将 c++ 中的字符串分成多行的可读性。

One place where this would be particularly desirable is when you're passing long string literals to a function (for example a sentence).

当您将长字符串文字传递给函数(例如一个句子)时,这是特别可取的一个地方。

回答by Eclipse

Don't put anything between the strings. Part of the C++ lexing stage is to combine adjacent string literals (even over newlines and comments) into a single literal.

不要在字符串之间放任何东西。C++ 词法分析阶段的一部分是将相邻的字符串文字(甚至在换行符和注释上)组合成一个文字。

#include <iostream>
#include <string>
main() {
  std::string my_val ="Hello world, this is an overly long string to have" 
    " on just one line";
  std::cout << "My Val is : " << my_val << std::endl;
}

Note that if you want a newline in the literal, you will have to add that yourself:

请注意,如果您想要文字中的换行符,则必须自己添加:

#include <iostream>
#include <string>
main() {
  std::string my_val ="This string gets displayed over\n" 
    "two lines when sent to cout.";
  std::cout << "My Val is : " << my_val << std::endl;
}

If you are wanting to mix a #defined integer constant into the literal, you'll have to use some macros:

如果您想将#defined 整数常量混合到文字中,则必须使用一些宏:

#include <iostream>
using namespace std;

#define TWO 2
#define XSTRINGIFY(s) #s
#define STRINGIFY(s) XSTRINGIFY(s)

int main(int argc, char* argv[])
{
    std::cout << "abc"   // Outputs "abc2DEF"
        STRINGIFY(TWO)
        "DEF" << endl;
    std::cout << "abc"   // Outputs "abcTWODEF"
        XSTRINGIFY(TWO) 
        "DEF" << endl;
}

There's some weirdness in there due to the way the stringify processor operator works, so you need two levels of macro to get the actual value of TWOto be made into a string literal.

由于 stringify 处理器运算符的工作方式,其中存在一些奇怪之处,因此您需要两级宏才能将 的实际值TWO转换为字符串文字。

回答by mkb

Are they both literals? Separating two string literals with whitespace is the same as concatenation: "abc" "123"is the same as "abc123". This applies to straight C as well as C++.

它们都是文字吗?用空格分隔两个字符串文字与串联"abc" "123"相同:与"abc123". 这适用于直接 C 以及 C++。

回答by rmeador

I don't know if it is an extension in GCC or if it is standard, but it appears you can continue a string literal by ending the line with a backslash (just as most types of lines can be extended in this manor in C++, e.g. a macro spanning multiple lines).

我不知道它是 GCC 中的扩展还是标准,但看起来您可以通过以反斜杠结束行来继续字符串文字(就像大多数类型的行都可以在 C++ 中扩展,例如跨越多行的宏)。

#include <iostream>
#include <string>

int main ()
{
    std::string str = "hello world\
    this seems to work";

    std::cout << str;
    return 0;
}