C++11 raw strings literals tutorial
来源:互联网 发布:有肉的耽美网络剧 编辑:程序博客网 时间:2024/04/27 14:43
Now, that I have a working system that can compile both regular expressions and raw strings literals, it is time to show you how you can further simplify the examples from the regex tutorial.
Basically a raw string literal is a string in which the escape characters (like \n \t or \" ) of C++ are not processed. A raw string literal starts with R"( and ends in )", let's see an in an example the difference between a normal string and a raw string in C++:
normal_str will be processed at compilation time so you will see three lines of text and an empty line. In the case of the variable raw_str which is a raw string literal, the compiler will not process the escape characters, so you will see a single line of text with a content identical with what you have in the C++ source code.
If you compile and run the above code, saved in a file named "raw_string_00.cpp", this is what you should see:
If you don't have clang and libc++ on your machine you could also compile the above code with g++-4.6.1, in this case you would use this line for compiling the code:
or, for gcc-4.7 and up:
The above piece can not be compiled with Visual Studio 2010 which lack support for raw string literals at the time of this writing.
A first application of the concept of a raw string is in simplifying the syntax of the regular expressions. The coder can put his effort in writing a regular expression conformal to the ECMAScript standard and not in ensuring that his regular expression will be correctly processed by the compiler.
Take as an example the regular expression used in the regex tutorial for checking if the user input is an integer number. Without raw strings this is how the code should look (and if you want to use it with VS 2010 you must keep it this way):
Using a raw string we can simplify the above piece of code, we can get rid of the escaping characters:
or, a more condensed version:
A pattern for matching a real numbers, see the regex tutorial, can be written this way:
- C++11 raw strings literals tutorial
- C++11中的raw string literals
- Python raw字符串(raw strings)
- Objective-C Literals
- Objective-C中的Literals
- Objective-C中的Literals
- Literals
- How to distinguish between strings in heap or literals?
- Objective-C字面量(Objective-C Literals)
- objective-c 2.0的字面量Literals
- #Objective - C - 字面量(Literals)整理
- Objective - C - 字面量(Literals)整理
- Python 原始字符串(raw strings)的用法
- Number/for...in/检索/||/&&/Syntactic Sugar/Default Parameters/Template Literals/Multiline Strings/this
- 代码简洁化——Objective-C中的Literals
- 关于字面量的理解(Objective-C Literals)
- sorted as raw strings, not alphanumerics 的个人理解?
- Python r 原始字符串(raw strings)的用法
- 在Linux中创建静态库.a和动态库.so
- 数据库添加外键错误:[Err] 1215 - Cannot add foreign key constraint 的解决
- jQuery_克隆和替换节点
- 使用Nexus搭建Maven私服
- 虚拟机模拟搭建VMware虚拟化平台
- C++11 raw strings literals tutorial
- IOCP 二进制协议 分包
- ——Character类、正则表达式、Math类、DateFormat类、
- 程序员为什么不适用一般的绩效考核
- 异常处理
- 字符串反转
- 磁盘基础知识笔记
- editText 一开始不自动对焦
- 密码技术之对称加密