原始字面量很容易理解,即不进行转义的完整字符串。
最近看了看Python,其中讲到了原始字符串。
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.
例如,原始字符串对于反斜杠不会做特殊的处理:
# Python 程序
print r'C:\nowhere'
即在Python中,原始字符串以r开头。
这样的功能C++会有吗?
C++11不愧称为modern c++,当然会提供原始字符串了。
但是Python里使用的是r,而c++11中使用的是R。
但是需要注意的是:
原始字符串字面量的定义为:R “xxx(raw string)xxx”
其中,原始字符串必须用括号()括起来,括号的前后可以加其他字符串,所加的字符串会被忽略,并且加的字符串必须在括号两边同时出现。
#include <iostream>
#include <string>
int main()
{
// 一个普通的字符串,'\n'被当作是转义字符,表示一个换行符。
std::string normal_str = "First line.\nSecond line.\nEnd of message.\n";
// 一个raw string,'\'不会被转义处理。因此,"\n"表示两个字符:字符反斜杠 和 字母n。
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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2013-07-11 MFC DestroyWindow窗口对象和窗口句柄的销毁
2012-07-11 VC++中的各种文件的作用
2012-07-11 setsockopt的常用选项设置及作用 .