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)
cout << endl inserts a new line and flushes the stream(output buffer), whereas cout << “\n” just inserts a new line.
The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
C++ manipulator ends function is used to insert a null terminating character on os. The ends manipulator does not take any argument whenever it is invoked. It causes a null character to the output. It is similar to '\0' or charT() in C++.
print is not, in c++. cout is the standard function to write something on the standard OUTput (screen, nowadays, printer before). Formely print, tells a programm to print on printer, cout on the screen.
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With