【发布时间】:2022-01-22 17:31:43
【问题描述】:
你能解释一下mbstate_t到底是什么吗?我已经阅读了cppreference description,但我仍然不明白它的用途。我所理解的是mbstate_t 是一些静态结构,对于有限的一组函数(如mbtowc()、wctomb() 等)可见,但我仍然对如何使用它感到困惑。我可以在 cppreference 示例中看到,在调用某些函数之前应该重置这个结构。假设,我想计算这样一个多语言字符串中的字符:
std::string str = "Hello! Привет!";
显然,str.size() 不能在这个例子中使用,因为它只是返回字符串中的字节数。但是这样的事情就可以了:
std::locale::global(std::locale("")); // Linux, UTF-8
std::string str = "Hello! Привет!";
std::string::size_type stringSize = str.size();
std::string::size_type nCharacters = 0;
std::string::size_type nextByte = 0;
std::string::size_type nBytesRead = 0;
std::mbtowc(nullptr, 0, 0); // What does it do, and why is it needed?
while (
(nBytesRead = std::mbtowc(nullptr, &str[nextByte], stringSize - nextByte))
!= 0)
{
++nCharacters;
nextByte += nBytesRead;
}
std::cout << nCharacters << '\n';
根据 cppreference 示例,在进入 while 循环之前,mbstate_t 结构应该通过调用 mbtowc() 来重置,所有参数都为零。这样做的目的是什么?
【问题讨论】:
标签: c++ unicode locale wchar-t wstring