类似于 print END <<结尾;在 C++ 中?

发布于 2024-08-07 23:24:33 字数 123 浏览 9 评论 0 原文

有没有办法

print << END
yadayadayada
END;

用 C++ 做类似 PHP 的事情? (多行、未转义、易于剪切和粘贴的流插入)

Is there anyway to do something like PHP's

print << END
yadayadayada
END;

in C++? (multi-line, unescaped, easy-to-cut-and-paste stream insertion)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

别挽留 2024-08-14 23:24:33

C++11 有原始的字符串文字

// this doesn't have '\n', but '\\' and 'n'
R"(yada"yadayada\n)" 

如果你需要这些括号,你也可以做到这一点,使用任何你想要的结束标记:

// the following will be "(yada)(yada)(yada)"
R"END((yada)(yada)(yada))END" 

它也适用于嵌入的新行:

// the following will be "\n(yada)\n(yada)\n(yada)\n"
R"END(
(yada)
(yada)
(yada)
)END" 

C++11 has raw string literals:

// this doesn't have '\n', but '\\' and 'n'
R"(yada"yadayada\n)" 

And if you need those parens, you can do that, too, using whatever you want for an end token:

// the following will be "(yada)(yada)(yada)"
R"END((yada)(yada)(yada))END" 

it also works with embedded new lines:

// the following will be "\n(yada)\n(yada)\n(yada)\n"
R"END(
(yada)
(yada)
(yada)
)END" 
长安忆 2024-08-14 23:24:33

这个答案对于现代 C++ 来说已经过时了 - 请参阅 sbi 的答案了解现代方式。

这是你能做的最好的事情:

std::cout <<
    "This is a\n"
    "multiline\n"
    "string.\n";

不像正确的heredoc那么方便,但也不可怕。

This answer is now out of date for modern C++ - see sbi's answer for the modern way.

This is the best you can do:

std::cout <<
    "This is a\n"
    "multiline\n"
    "string.\n";

Not as convenient as a proper heredoc, but not terrible.

够运 2024-08-14 23:24:33

在 C++ 中,通常不认为将大量数据放入源代码中是代码风格,因此没有一种奇特的语言方法可以做到这一点。

将文本放入外部文件(例如文本文件)通常更灵活,然后它不会绑定到已编译的可执行文件中。

如果您确实希望将文本绑定到可执行文件中,那么(取决于您的平台)您通常可以使用某种形式的资源支持,或者带有“incbin”样式指令的汇编器来命名包含所需文本的数据区域。

或者,您可以使用外部实用程序(例如 xxd -i)从给定的输入文件编译命名的 C 样式数组。然后可以将生成的文件与其余源代码一起编译。

In C++, it's not usually considered code style to put large amounts of data into source code so there isn't a fancy language way to do it.

It is usually more flexible to put the text into an external file (such as a text file), then it isn't bound into the compiled executable.

If you do want the text to be bound into the executable then (depending on your platform) you can often use some form of resource support, or an assembler with an 'incbin' style directive to give name to a data area with the text that you want.

Alternatively, you can use an external utility (such as xxd -i) to compiler a named C style array from a given input file. The generated file can then be compiled with the rest of the source code.

冰火雁神 2024-08-14 23:24:33

是的。 http://en.cppreference.com/w/cpp/language/string_literal

const char* s1 = R"foo(
Hello
World
)foo";
//same as
const char* s2 = "\nHello\nWorld\n";

无论它是否是最佳实践,C++11 都能很好地满足您的需求。

Yes. http://en.cppreference.com/w/cpp/language/string_literal

const char* s1 = R"foo(
Hello
World
)foo";
//same as
const char* s2 = "\nHello\nWorld\n";

Whether or not it's best-practice, C++11 does pretty well exactly what you want.

行雁书 2024-08-14 23:24:33

你可以这样做:

std::cout << "First line\n"
"second line\n"
"third line\n" ;

这就是你可以用 C++ 做的最好的事情。

You can do like this:

std::cout << "First line\n"
"second line\n"
"third line\n" ;

And that's the best you can do with C++.

森末i 2024-08-14 23:24:33

C++ 中不存在 HEREDOC 这样的东西。

你能做到的

cout << 
"yadayadayada"
"yadayadayada"
<< endl;

就是你能做到的最好的。但是您仍然必须转义特殊字符,例如 \,",'。

方法是将文本作为资源包含到可执行文件中或从外部文件加载它。

There is not such thing as a HEREDOC in C++.

You can do

cout << 
"yadayadayada"
"yadayadayada"
<< endl;

That is the best you can get. But you still has to escape special chars like \,",'.

The way would it be to include the text as an ressource into the executable or load it from an external file.

风透绣罗衣 2024-08-14 23:24:33

行末尾的斜杠表示下一行是字符串的延续。这将引导您使用替代语法,与其他建议相比,您可能更喜欢也可能不喜欢该语法:

std::cout << "\
This is a\n\
multiline\n\
string.\
";

优点:您不必将每一行用引号引起来;从其他来源导入文本时,您只需编辑每一行的一端,而不是两端(更适合宏);您可以准确地看到结果字符串的外观,包括前导空格。

缺点:您仍然必须显式包含 \n 来换行,而且我不喜欢失去漂亮且漂亮地缩进代码的能力。 HEREDOC 语法也受到此功能的影响,因此也许这不会打扰您。

就我个人而言,这不是我喜欢的语法。

A slash at the end of a line means the next line is a continuation of the string. This leads you to an alternate syntax that you may or may not prefer over other suggestions:

std::cout << "\
This is a\n\
multiline\n\
string.\
";

The advantages: you don't have to wrap each line in quotes; when importing text from some other source you only have to edit one end of each line, not both ends (more macro friendly); and you can see exactly how the resulting string will look, including leading spaces.

The disadvantages: you still have to explicitly include \n for newlines, and I don't like losing the ability to indent my code nice and pretty. HEREDOC syntax also suffers from this feature, so perhaps that won't bother you.

Personally, this isn't a syntax I like.

美胚控场 2024-08-14 23:24:33

如果我理解正确的话,我相信你想要这个:

#include <iostream>
...
std::cout << std::endl << "yadayadayada" << std::endl;

If I understand you correctly, I believe you want this:

#include <iostream>
...
std::cout << std::endl << "yadayadayada" << std::endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文