c++ - 为什么 towlower() 函数不将 Я 转换为小写 я?

标签 c++ unicode locale lowercase wchar-t

函数 towlower() 似乎在 Visual Studio 2012 中不起作用。这是一个示例:

#include <string>
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <wctype.h>

using namespace std;

int main()
{
    _setmode(_fileno(stdout), _O_U8TEXT);
    wcout << (wchar_t)towlower(L'Я') << endl;
    system("pause");
    return 0;
}

字符保持大写。以前在这里问过类似的问题,但我找不到任何解决方案。

我可以使用另一种方法来更改为小写吗?

最佳答案

使用 tolower 的语言环境感知版本,但不要忘记也设置 C 语言环境。

例如:

#include <clocale>
#include <locale>
#include <iostream>

int main()
{
    std::setlocale(LC_CTYPE, "");
    std::wcout << L"The letter is: " << L'Я' << L" => "
               << std::tolower(L'Я', std::locale("")) << std::endl;
}

这打印:

The letter is: Я => я

在 iostream 中使用语言环境是一件棘手的事情,这背后隐藏着整个潘多拉魔盒。例如,您可以imbue 带有语言环境的流,并且您可以同时管理多个语言环境,特别是您可以为每个线程设置一个语言环境(这对于有状态的字符串编码转换可能是必需的)...有人应该写一本关于它的书(或者使用 Boost.Locale)。

关于c++ - 为什么 towlower() 函数不将 Я 转换为小写 я?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889784/

相关文章:

c++ - lambda(或函数)C++ 中的静态初始化

java - 在 Java 中用 String 中的字符替换 Unicode 字符代码

java - 如何使用 JNI 从 C++ 向 CallObjectMethod 传递多个参数

C++; MPI : Send struct with vector of vectors through MPI

python - 中文统一码问题?

c++ - 将 TEXTRANGE 结构的 lpstrtext 成员转换为 MultiByteCharacterSet

c++ - 设置区域设置后输入 4 位数字时,operator>> 返回失败

android - 立即更改语言环境

grails - 如何在考虑当前用户区域设置的 Grails Controller 中解析 BigDecimal?

c++ - 使用 std::move 在开头插入 vector 的中间元素不起作用