[Modern C++] raw string literal

Time:2019-10-9

Today, a C++ 11 introduces a small but very useful feature, raw string literal.

background

In almost every program, it is applied to string literals. However, the traditional grammar of literal quantities of strings is not friendly to the support of literal quantities with special characters. It introduces some escape characters to represent these special characters in strings, such as:

\ a Ring Bell (BEL)
\ B Backspace (BS)
\ f Page Change (FF)
\ n newline (LF)
\ R Return (CR)
\ t Horizontal Tabulation (HT)
\ V Vertical Tabulation (VT)
\ Backslash
\ Question mark character
 Single quotation mark character
\ "Double quotation mark character
\ 0 empty character (NULL)

The problem that this poses is not too big for simple strings, but it is obviously ineffective for some special applications, such as regular expressions:
\\d{3}-\\d{8}|\\d{4}-\\d{7}
Because regular expressions are heavily used\Backslashes are used as the beginning of escape sequences, so they need to be used in strings\\Although this grammar can solve the problem, it is obvious that the readability is very poor. If the application scenario is more complex, it is estimated that many people will spend a lot of time to count the backslash.

If it can be expressed directly as\d{3}-\d{8}|\d{4}-\d{7}How wonderful!

grammar

C++ 11 finally acted, introducing the original string. The most basic usage isR"(...)"ByRBeginning, surrounded by double quotation marks(...)The actual character sequence is within parentheses, which are delimiters of the character sequence. Of course, the left and right parentheses correspond in the first place.

The “raw” character in a string is one or two, and it won’t escape you. That is to say, traditional"\n"Except for the end character of the string, only the newline character is included, while the original stringR"(\n)"It contains backslashes and charactersnThis is a clear difference.

From now on, it seems that the problem has been solved very well, but if the character sequence contains)"For exampleR "(coordinates:"(x, y)""At this point, the compiler is confused because"(Two correspond to each other.)"This is the case. In this case, we can choose other delimiters, such as:R"& (coordinates:"(x, y)"&"This is the case. The grammar is as follows:
R"delim(...)delim"delimThe selection is more flexible, up to 16 characters, and not parentheses, blanks, control characters and backslashes.

application

The application of the original string in the regular expression is given in the background, but the application scenario goes beyond that, for example:

  • Windows File Path

R"(C:\ProgramFiles\xx\xx\xx.exe)"

  • Json string

R"({"name":"xx","age":10})"

Example

The first question in Luogu is:
[Modern C++] raw string literal

This problem can be solved by other techniques. Here we only give the solution using the original string.

#include<iostream>

int main() {
    std::cout << R"(                ********
               ************
               ####....#.
             #..###.....##....
             ###.......######              ###            ###
                ...........               #...#          #...#
               ##*#######                 #.#.#          #.#.#
            ####*******######             #.#.#          #.#.#
           ...#***.****.*###....          #...#          #...#
           ....**********##.....           ###            ###
           ....****    *****....
             ####        ####
           ######        ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
##########################################    #----------#
#.....#......##.....#......##.....#......#    #----------#
##########################################    #----------#
#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
##########################################    ############ )";
}

Through the examples above, we can also find two other advantages of the original string:

  1. A new line can be well preserved
    Other ways require manual addition\nRealize line change.
  2. Convenient Copy String
    Replace copied strings directlyR"(...)"In...The task is completed and very convenient. Others require too much interference with copied strings, which is very error-prone.

Please continue to pay attention to my public document.
[Modern C++] raw string literal

Recommended Today

Detailed explanation of spring cloud ribbon load balancing Shenkeng that cannot be connected through the server name

First, the problem. Take the Eureka cluster and the client calls the service through the ribbon. The ribbon side reports the following exceptions java.net.UnknownHostException: SERVICE-HI java.lang.IllegalStateException: No instances available for SERVICE-HI java.lang.IllegalStateException: Request URI does not contain a valid hostname: http://SERVICE-HI com.netfix.discovery.shared.taransport.TransportException: Cannot execute request on any known server The spring cloud version is messy, […]