cpp11Unicode 字符串支持
时间: 2023-05-31 19:03:19 浏览: 72
C++11 引入了对 Unicode 字符串的支持,包括以下几个方面:
1. Unicode 字符串字面值:使用前缀 u8、u、U、L 分别表示 UTF-8、UTF-16、UTF-32、宽字符编码的 Unicode 字符串字面值。
```cpp
// UTF-8 字符串字面值
const char* u8str = u8"Hello World!";
// UTF-16 字符串字面值
const char16_t* ustr = u"Hello World!";
// UTF-32 字符串字面值
const char32_t* Ustr = U"Hello World!";
// 宽字符编码的 Unicode 字符串字面值
const wchar_t* wstr = L"Hello World!";
```
2. Unicode 字符串类型:新增了名为 u16string 和 u32string 的字符串类型,分别代表 UTF-16 和 UTF-32 编码的 Unicode 字符串。
```cpp
#include <iostream>
#include <string>
int main()
{
// UTF-16 编码的 Unicode 字符串
std::u16string u16str = u"Hello World!";
// UTF-32 编码的 Unicode 字符串
std::u32string u32str = U"Hello World!";
std::cout << u16str << std::endl;
std::cout << u32str << std::endl;
return 0;
}
```
3. Unicode 编码转换:新增了头文件 <codecvt>,提供了 std::wstring_convert 类模板和 std::codecvt 类模板,用于在不同的编码之间进行转换。
```cpp
#include <iostream>
#include <string>
#include <codecvt>
int main()
{
// UTF-16 编码的 Unicode 字符串
std::u16string u16str = u"Hello World!";
// 将 UTF-16 编码的字符串转换为 UTF-8 编码的字符串
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> cvt;
std::string utf8str = cvt.to_bytes(u16str);
std::cout << utf8str << std::endl;
return 0;
}
```
以上就是 C++11 对 Unicode 字符串的支持。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)