C++11新特性-R原生字符串

本文最后更新于:2021年11月29日 晚上

前情提要

众所周知,C++字符串中有些字符具有特殊含义,需要转义(如'\\' '\"')

特别是在表示Windows路径时,极其麻烦('\\'含量超标)

那么有什么办法可以告诉编译器取消转义呢?

最懂我

没错,原生字符串(Raw string literal)现已加入C++11豪华午餐

只需要使用R前缀,即可取消转义

专业释义

prefix(optional) R”d-char-sequence(optional)(r-char-sequence(optional)) d-char-sequence(optional) since C++11

Raw string literal. Used to avoid escaping of any character. Anything between the delimiters becomes part of the string. prefix, if present, has the same meaning as described above. The terminating d-char-sequence is the same sequence of characters as the initial d-char-sequence.

From String literal - cppreference.com

本地翻译

什么意思呢

就是

1
R"(原始字符串)" == "转义的字符串"

举个例子

1
2
3
string str1 = "E:\\test.jpg";
string str2 = R"sth(E:\test.jpg)sth";//sth在括号前后 用来描述字符串(可选)
//输出: E:\test.jpg

也就是()内的字符串原样输出(包括换行、空白字符),不进行转义,所见即所得

妈妈再也不用担心我的Windows路径

ちょっと

等下 忘记吐槽了

C++11 这都2021年了 10年了 算个卵新特性

这叫旧特性 焯

peace


C++11新特性-R原生字符串
https://mrbeancpp.github.io/2021/11/29/C++11新特性-R原生字符串/
作者
MrBeanC
发布于
2021年11月29日
许可协议