1.1新类型
C++11新增类型long long和unsigned long long,以支持64位(或者更宽)整型;新增了char16_t和char32_t以支持16位和32为字符表示;还新增了原始字符串。
1.1.1原始字符串(原始字面量)
在C++ Prime Plus(第六版) 104页中介绍了原始(raw)字符串。
在编程中假如遇到了字符串“\n”,这里表示换行的意思,即"\n"表示的是一个字符——换行。那如果我们想表示一个'\'一个'\'该怎么办呢?在C语言中是这样写"\\n"。一个这样写很容易,但一个字符串里有多个\与字母的组合时我们只想让其表达字面意思怎么办呢?这时原始字符串出场了。定义方式为R “xxx(原始字符串)xxx” ;比如:
cout<<R"(jim "king" Tutt uses "\n" instead of endl.)"<<'\n'
上述代码将显示以下内容
jim "king" Tutt uses "\n" instead of endl.
代码举例:
#include<iostream>
#include<string>
using namespace std;
int main(int argc,char const *argv[])
{
string str = "D:\C++\hello world\test.cpp";
cout << str << endl;
string str = "D:\\C++\\hello world\\test.cpp";
cout << str1 << endl;
string str = R"(D:\C++\hello world\test.cpp)";
cout << str2 << endl;
return 0;
}
输出结果为:
D:C++hello world est.cpp
D:\C++\hello world\test.cpp
D:\C++\hello world\test.cpp
在 D:\C++\hello world\test.cpp 中 \h 和 \C 转译失败,对应的字符会原样输出。
在 D:\C++\hello world\test.cpp中路径的间隔符为 \ 但是这个字符又是转译字符,因此需要使用转译字符将其转译,最终才能得到一个没有特殊含义的普通字符 \。
在 R"(D:\C++\hello world\test.cpp)"使用了原始字面量 R()中的内容就是描述路径的原始字符串,无需做任何处理。
string str = R"abc(D:\C++\hello world\test.cpp)abc";
即R后面的两处xxx字符要完全一样。否则编译报错。
287

被折叠的 条评论
为什么被折叠?



