C++11新特性探索:原始字符串字面值(raw string literal)

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: 原始字符串字面值(raw string literal)是C++11引入的新特性。

原始字符串字面值(raw string literal)是C++11引入的新特性。


原始字符串简单来说,“原生的、不加处理的”,字符表示的就是自己(所见即所得),引号、斜杠无需 “\” 转义,比如常用的目录表示,引入原始字符串后,非常方便。


格式如下:

R"(原始字符串)";

废话不多说,上代码:


比如显示RedistList目录,用redist_path1显然是错的,C++ 11之前需要用redist_path2这种方式转义,当我们引入原始字符串的话,非常方便。


顺便来个流行的佛祖保佑,永无bug。

#include "stdafx.h"
#include <iostream>
#include<string>
int main()
{
  std::string redist_path1 = "C:\Program Files (x86)\Microsoft.NET\RedistList";
  std::string redist_path2 = "C:\\Program Files (x86)\\Microsoft.NET\\RedistList";
  std::string redist_path3 = R"(C:\Program Files (x86)\Microsoft.NET\RedistList)";
  std::string redist_path4 = R"(C:\\Program Files (x86)\\Microsoft.NET\\RedistList)";
  std::cout << "redist_path1: "<< redist_path1 << std::endl;
  std::cout << "redist_path2: " << redist_path2 << std::endl;
  std::cout << "redist_path3: " << redist_path3 << std::endl;
  std::cout << "redist_path4: " << redist_path4 << std::endl;
  std::string fozu = R"(
                            _ooOoo_
                           o8888888o
                           88" . "88
                           (| -_- |)
                            O\ = /O
                        ____/`---'\____
                      .   ' \\| |// `.
                       / \\||| : |||// \
                     / _||||| -:- |||||- \
                       | | \\\ - /// | |
                     | \_| ''\---/'' | |
                      \ .-\__ `-` ___/-. /
                   ___`. .' /--.--\ `. . __
                ."" '< `.___\_<|>_/___.' >'"".
               | | : `- \`.;`\ _ /`;.`/ - ` : | |
                 \ \ `-. \_ __\ /__ _/ .-` / /
         ======`-.____`-.___\_____/___.-`____.-'======
                            `=---='
    )";
  std::cout << fozu << std::endl;
  return 0;
}

编译运行查看结果:

2ec4742bd7034d58adb42cdeb17f89da.png

相关文章
|
9天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
189 100
|
9天前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
206 99
|
12天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
12天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
2月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
212 92
|
3月前
|
C语言 C++
【实战指南】 C/C++ 枚举转字符串实现
本文介绍了在C/C++中实现枚举转字符串的实用技巧,通过宏定义与统一管理枚举名,提升代码调试效率并减少维护错误。
247 54
|
3月前
|
自然语言处理 Java Apache
在Java中将String字符串转换为算术表达式并计算
具体的实现逻辑需要填写在 `Tokenizer`和 `ExpressionParser`类中,这里只提供了大概的框架。在实际实现时 `Tokenizer`应该提供分词逻辑,把输入的字符串转换成Token序列。而 `ExpressionParser`应当通过递归下降的方式依次解析
218 14
|
4月前
|
对象存储 C++ 容器
c++的string一键介绍
这篇文章旨在帮助读者回忆如何使用string,并提醒注意事项。它不是一篇详细的功能介绍,而是一篇润色文章。先展示重载函数,如果该函数一笔不可带过,就先展示英文原档(附带翻译),最后展示代码实现与举例可以直接去看英文文档,也可以看本篇文章,但是更建议去看英文原档。那么废话少说直接开始进行挨个介绍。
86 3