Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 387
Nona Urbiz Avatar asked Oct 10 '09 21:10

Nona Urbiz


People also ask

What is cout << endl in C?

cout << endl inserts a new line and flushes the stream(output buffer), whereas cout << “\n” just inserts a new line.

What is cout << in C?

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.

What is << end in C++?

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

Is cout same as print?

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.


2 Answers

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" 
like image 83
sbi Avatar answered Sep 29 '22 01:09

sbi


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.

like image 20
RichieHindle Avatar answered Sep 29 '22 00:09

RichieHindle