gpt4 book ai didi

c++ - Unicode 字符串上的 std::string 和 std::map 操作

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:14 32 4
gpt4 key购买 nike

我想了解正则std::stringstd::map如果 Unicode 代码单元出现在字符串中,操作将处理它们。

示例代码:

    include <iostream>
#include "sys/types.h"

using namespace std;

int main()
{

std::basic_string<u_int16_t> ustr1(std::basic_string<u_int16_t>((u_int16_t*)"ยฤขฃ", 4));
std::basic_string<u_int16_t> ustr2(std::basic_string<u_int16_t>((u_int16_t*)"abcd", 4));

for (int i = 0; i < ustr1.length(); i++)
cout << "Char: " << ustr1[i] << endl;

for (int i = 0; i < ustr2.length(); i++)
cout << "Char: " << ustr2[i] << endl;

if (ustr1 == ustr2)
cout << "Strings are equal" << endl;

cout << "string length: " << ustr1.length() << "\t" << ustr2.length() << endl;
return 0;
}

字符串包含泰文字符和 ascii 字符,以及使用 basic_string<u_int16_t> 的意图是为了便于存储不能容纳在单个字节中的字符。代码在 Linux 机器上运行,其编码类型为 en_US.UTF-8 .输出是:

$ ./a.out
Char: 47328
Char: 57506
Char: 42168
Char: 47328
Char: 25185
Char: 25699
Char: 17152
Char: 24936
string length: 4 4

几个问题:

  1. 输出中的字符值是否对应于en_US.UTF-8代码点?如果不是,它们是什么?

  2. std::string运营商喜欢 == , != , <等,能够使用 Unicode 代码点吗?如果是这样,是否只是比较相应位置的每个代码点?会 std::map在类似的线路上工作?

  3. 将语言环境更改为 UTF-16 是否会导致字符串存储为 UTF-16 代码点?

谢谢!

最佳答案

I would like to understand how regular std::string and std::map operations deal with Unicode code units should they be present in the string.

他们没有。

std::stringchar 的序列s 或 bytes。它不是考虑任何 编码的“高级”字符串。你必须自己做,例如通过使用专用于该目的的库,例如 ICU。

std::string 切换(即 std::basic_string<char> )到 std::basic_char<u_int16_t>不会改变这一点;它只是意味着您有一系列“宽”字符。

std::map与此完全无关。

进一步阅读:

关于c++ - Unicode 字符串上的 std::string 和 std::map 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36740126/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com