C++11中的raw string literals

来源:互联网 发布:tcl人工智能电视 编辑:程序博客网 时间:2024/04/29 22:20

作为一名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啊。






0 0
原创粉丝点击