Splitting C ++ lines into multiple lines (code syntax, not analysis)

Not to be confused with how to parse string parsing, for example:
Split string in C ++?

I'm a little confused how to split a string into multiple lines in 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 understand that I could use the std::string append() method, but I was wondering if there are shorter / more elegant ones (e.g. more pythonic, although obviously triple quotes, etc. are not supported in C ++), the way to break lines in C ++ into several lines for readability.

One place where this would be especially desirable is when you pass long string literals to a function (like a sentence).

+47
c ++ string syntax coding-style readability
Oct 04 '10 at 21:13
source share
3 answers

Do not put anything between the lines. Part of the C ++ vocabulary phase is to combine adjacent string literals (even over new lines and comments) into one literal.

 #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 to use a new line in a literal, you will have to add this:

 #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 want to mix the #define d integer constant into a literal, you will need to use some macros:

 #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 kind of weirdness about how the processor gating operator works, so you need two macro levels to get the actual TWO value to convert to a string literal.

+83
04 Oct '10 at 21:15
source share

Are they both literals? Separating two string literals with a space is the same as concatenation: "abc" "123" same as "abc123" . This applies to both direct C and C ++.

+7
04 Oct '10 at 21:15
source share

I don’t know if this is an extension in GCC or standard, but it looks like you can continue the string literal by ending the string with a backslash (just like most line types can be extended in this estate in C ++, for example, a macro, spanning multiple lines).

 #include <iostream> #include <string> int main () { std::string str = "hello world\ this seems to work"; std::cout << str; return 0; } 
+4
04 Oct 2018-10-10T00:
source share



All Articles