Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignore escape sequence c++

Tags:

c++

string

I have tried searching google but could not find answer, can anyone tell that how to ignore escape character stored in string . I am using an array which holds different characters like this

string str[]={"| | / / ||","| |/ / ||", "| | ( \ \ \ \`_."}

Error message from compiler:

"unknown escape sequence" at

like image 897
user3215228 Avatar asked Feb 04 '14 21:02

user3215228


1 Answers

As @Zac has already pointed out, you can escape the \ to avoid the problem. Another way you may find cleaner1 would be to use raw string literals:

string str[]={R"(| | / / ||)",R"(| |/ / ||)", R"(| | ( \ \ \ \`_.)"};

Since it's not apparent from that sample, I'll point out one additional detail: if your strings might contain a ) character, you can add more delimiters before the opening parenthesis, that also have to be matched next to the closing parenthesis, something like this:

string s = R"!(This (might be) an MS-DOS path\to\a\string)!";

Edit: Oh, one other minor detail: a raw string literal produces the same type of result as a normal string literal. You can intermix the two freely. For example, I could have left the first two literals above as normal literals, and only made the third one raw, since it's the only one that contains a backslash. For initializing an array (or other collections), I prefer to make them all raw if any of them is going to be, but that's strictly personal preference for the sake of consistency, not something the language requires.


1. Just in case: this is new with C++ 11, so if you're using an older compiler, this may not be supported.

like image 80
Jerry Coffin Avatar answered Oct 02 '22 03:10

Jerry Coffin