Something like print END << END; in C++?

2019-02-09 05:11发布

问题:

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)

回答1:

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.



回答2:

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" 


回答3:

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.



回答4:

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.



回答5:

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++.



回答6:

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

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


回答7:

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.



回答8:

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.