前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >探索C++11新特性:原始字符串字面值(raw string literal)

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

原创
作者头像
音视频牛哥
发布2022-03-17 01:20:05
发布2022-03-17 01:20:05
1.8K00
代码可运行
举报
运行总次数:0
代码可运行

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

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

格式如下:

代码语言:javascript
代码运行次数:0
运行
复制
R"(原始字符串)";

废话不多说,上代码:

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

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

代码语言:javascript
代码运行次数:0
运行
复制
#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;
}

编译运行查看结果:

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档