C++11中的raw string literals

转自https://www.cnblogs.com/myd7349/p/raw_string_literals_in_cplusplus_11.html

作为一名C++书看得少得可怜的新手,我一直没有勇气去系统地学习一下C++ 11添加的新特性。不过,平日里逛论坛,阅读大犇们的博客,倒是了解了一些。比如,这个帖子:

如何绕过g++ 4.8.1那个不能在宏里面使用R"(...)"的bug?

看到形如:R"" 这样的写法,相信学过Python的童鞋会感到似曾相识。Python支持所谓的“raw string”。Python文档这样介绍raw string:

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.
从这段文字中我们可以看出,raw string最大的特点就是:它不会对反斜杠'\'进行特殊的转义处理。
那么,它的这一特性有什么好处呢?
不用正则,不知raw string大法好!我们知道,正则表达式里,有很多元字符,当没有raw string时,我们需要在书写正则表达式的时候使用'\\'来表示元字符里的'\',这样将导致正则表达式变得冗长,而且可读性也会降低。

C++ 11中的raw string,简化了我们在使用regex库时正则表达式的书写。下面是我找到的一些资料:

C++11 raw strings literals tutorial
Wikipedia: C++ 11 # New String Literals

示例代码:

复制代码

#include <iostream>
#include <string>

int main()
{
    // 一个普通的字符串,'\n'被当作是转义字符,表示一个换行符。
    std::string normal_str = "First line.\nSecond line.\nEnd of message.\n";
    // 一个raw string,'\'不会被转义处理。因此,"\n"表示两个字符:字符反斜杠 和 字母n。
    // 注意其语法格式,稍后会介绍C++ 11中为什么会采用这种语法格式来表达一个raw string。
    std::string raw_str = R"(First line.\nSecond line.\nEnd of message.\n)";

    std::cout << normal_str << std::endl;
    std::cout << raw_str << std::endl;
    std::cout << R"foo(Hello, world!)foo" << std::endl;
    
    // raw string可以跨越多行,其中的空白和换行符都属于字符串的一部分。
    std::cout <<R"(
                   Hello,
                   world!
                   )" << std::endl;

    return 0;
}

复制代码

上面这段代码及其中注释大致讲解了C++ 11中的raw string的特点。但是为什么我们要在字符串中使用一对小括号呢?
我找到了如下资料:

What is the rationale for parenthesis in C++11's raw string literals R“(…)”?

C++11 FAQ中文版:原生字符串标识

示例代码:

复制代码

#include <iostream>

int main()
{
    // 下面两行代码意图说明C++ 11采用一对圆括号以及自定义分割字符串来表示raw string的原因。
    // 1.
    // 如果没有一对圆括号及空的分割字符串做定界处理,R"""将会出现语法错误。Python中,r"""也不会是一个合法的
    // raw string literal。
    std::cout << R"(")" << std::endl; // 输出一个双引号:"
    // 2.
    // 自定义分割字符串为:delimiter。分割字符串的长度以及其中包含的字符集,都有明文规定。维基百科:
    // The string delimiter can be any string up to 16 characters in length, including the empty string. 
    // This string cannot contain spaces, control characters, '(', ')', or the '\' character. 
    // 
    // 如果不使用自定义分割字符串,这里:R"()")"编译器无法识别raw string在何处结束。自定义分割字符串的用途
    // 维基百科中也有介绍:
    // The use of this delimiter string allows the user to have ")" characters within raw string literals.
    std::cout << R"delimiter()")delimiter" << std::endl; // 输出:)"

    return 0;
}

复制代码

所以,小伙伴们以后在C++ 11中书写正则表达式的时候,记得用raw string literals啊。

 

### C++ Raw String Usage In C++, raw strings provide a convenient way to handle string literals containing backslashes (`\`), quotes (`"`), or other special characters without needing escape sequences. A raw string is prefixed with `R"delimiter(`)`, where `delimiter` can be any sequence of characters used to mark the end of the string. #### Basic Syntax Example A simple example demonstrates how raw strings work: ```cpp #include <iostream> using namespace std; int main() { cout << R"(This is a "raw" string\nIt spans multiple lines)" << endl; } ``` The output will preserve both double quotes and newline characters as literal text rather than interpreting them specially[^1]. #### Using Delimiters When dealing with complex cases involving parentheses within the string content, delimiters help avoid conflicts by adding custom markers around the string definition: ```cpp string sqlQuery = R"DELIM(SELECT * FROM users WHERE name LIKE 'O'Connor')DELIM"; cout << sqlQuery << endl; ``` Here, `DELIM` serves as an arbitrary delimiter chosen not present inside the actual SQL query itself[^2]. #### Practical Application Scenario Raw strings prove particularly useful when working with regular expressions which often contain numerous backslashes requiring escaping under normal circumstances: ```cpp regex pattern(R"(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z]{2,}\b)"); // This regex matches email addresses. ``` Using raw strings simplifies such patterns significantly compared to traditional methods relying heavily on manual escapes[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值