C++11 raw strings literals tutorial
2017-01-09 01:48
375 查看
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:
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++:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 int main() 7 { 8 string normal_str="First line.\nSecond line.\nEnd of message.\n"; 9 string raw_str=R"(First line.\nSecond line.\nEnd of message.\n)"; 10 cout<<normal_str<<endl; 11 cout<<raw_str<<endl; 12 return(0); 13 }
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:
1 sol@sol:~$ clang++ -std=c++11 -stdlib=libc++ raw_string_00.cpp 2 sol@sol:~$ ./a.out 3 First line. 4 Second line. 5 End of message. 6 7 First line.\nSecond line.\nEnd of message.\n 8 sol@sol:~$
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:
1 g++ -std=c++0x raw_string_00.cpp
or, for gcc-4.7 and up:
1 g++ -std=c++11 raw_string_00.cpp
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):
1 regex integer("(\\+|-)?[[:digit:]]+");
Using a raw string we can simplify the above piece of code, we can get rid of the escaping characters:
1 string raw_pattern=R"((\+|-)?[[:digit:]]+)"; 2 regex integer(raw_pattern);
or, a more condensed version:
1 regex integer(R"((\+|-)?[[:digit:]]+)");
A pattern for matching a real numbers, see the regex tutorial,
can be written this way:
1 ... 2 string input; 3 regex rr(R"(((\+|-)?[[:digit:]]+)(\.(([[:digit:]]+)?))?((e|E)((\+|-)?)[[:digit:]]+)?)"); 4 cout<<"Give me a real number!"<<endl; 5 cin>>input; 6 if(regex_match(input,rr)) 7 cout<<"float"<<endl; 8 else 9 { 10 cout<<"Invalid input"<<endl; 11 } 12 ...
相关文章推荐
- C++11中的raw string literals
- C++11中的raw string literals
- C++11中的新特性raw介绍
- Number/for...in/检索/||/&&/Syntactic Sugar/Default Parameters/Template Literals/Multiline Strings/this
- Why can't raw strings end with a backslash
- sorted as raw strings, not alphanumerics 的个人理解?
- How to distinguish between strings in heap or literals?
- C++11 Tutorial: Introducing the Move Constructor and the Move Assignment Operator
- Entity Framework Tutorial Basics(39):Raw SQL Query
- A brief programming tutorial in C for raw sockets
- 学习c++11和boost
- Java 警告------Type safety: The method add(Object) belongs to the raw type Vector
- LeetCode 406. Queue Reconstruction by Height【H排序,c++11】
- C++11 中值得关注的几大变化
- C++11新特性
- Swift3.0 中 Strings/Characters 闲聊
- Android中资源文件夹res/raw和assets的使用
- AngularJS Tutorial(3)from w3school
- Android studio 如何将字符串添加到values-Strings.xml文件中
- Latent Semantic Analysis (LSA) Tutorial