C++11新特性探索:原始字符串字面值(raw string literal)
生活随笔
收集整理的這篇文章主要介紹了
C++11新特性探索:原始字符串字面值(raw string literal)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原始字符串字面值(raw string literal)是C++11引入的新特性。
原始字符串簡單來說,“原生的、不加處理的”,字符表示的就是自己(所見即所得),引號(hào)、斜杠無需 “\” 轉(zhuǎn)義,比如常用的目錄表示,引入原始字符串后,非常方便。
格式如下:
R"(原始字符串)";廢話不多說,上代碼:
比如顯示RedistList目錄,用redist_path1顯然是錯(cuò)的,C++ 11之前需要用redist_path2這種方式轉(zhuǎn)義,當(dāng)我們引入原始字符串的話,非常方便。
順便來個(gè)流行的佛祖保佑,永無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_o8888888o88" . "88(| -_- |)O\ = /O____/`---'\____. ' \\| |// `./ \\||| : |||// \/ _||||| -:- |||||- \| | \\\ - /// | || \_| ''\---/'' | |\ .-\__ `-` ___/-. /___`. .' /--.--\ `. . __."" '< `.___\_<|>_/___.' >'"".| | : `- \`.;`\ _ /`;.`/ - ` : | |\ \ `-. \_ __\ /__ _/ .-` / /======`-.____`-.___\_____/___.-`____.-'======`=---=')";std::cout << fozu << std::endl;return 0; }編譯運(yùn)行查看結(jié)果:
?
總結(jié)
以上是生活随笔為你收集整理的C++11新特性探索:原始字符串字面值(raw string literal)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【NLP】Transformer自注意机
- 下一篇: springMVC,aop管理log4j