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

原始字符串字面值(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;
}

编译运行查看结果:

 

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值