7

我正在做一个C++需要从中获取数据的项目unicode text。我有一个问题,我不能降低一些unicode character。我wchar_t用来存储从 unicode 文件中读取的 unicode 字符。之后,我_wcslwr用来降低wchar_t字符串。还有很多情况仍然不低,例如:

Đ Â Ă Ê Ô Ơ Ư Ấ Ắ Ế Ố Ớ Ứ Ầ Ằ Ề Ồ Ờ Ừ Ậ Ặ Ệ Ộ Ợ Ự

哪个小写是:

đ â ă ê ô ơ ư ấ ắ ế ố ớ ứ ầ ằ ề ồ ờ ừ ậ ặ ệ ộ ợ ự 

我已经尝试过tolower,它仍然无法正常工作。

4

1 回答 1

5

如果您只调用tolower,它将std::tolower从标题调用,该标题clocale将仅调用tolowerfor ansi 字符。

正确的签名应该是:

template< class charT >
charT tolower( charT ch, const locale& loc );

下面是 2 个运行良好的版本:

#include <iostream>
#include <cwctype>
#include <clocale>
#include <algorithm>
#include <locale>

int main() {
    std::setlocale(LC_ALL, "");
    std::wstring data = L"Đ Â Ă Ê Ô Ơ Ư Ấ Ắ Ế Ố Ớ Ứ Ầ Ằ Ề Ồ Ờ Ừ Ậ Ặ Ệ Ộ Ợ Ự";
    std::wcout << data << std::endl;

    // C std::towlower
    for(auto c: data)
    {
        std::wcout << static_cast<wchar_t>(std::towlower(c));
    }
    std::wcout << std::endl;

    // C++ std::tolower(charT, std::locale)
    std::locale loc("");
    for(auto c: data)
    {
        // This is recommended
        std::wcout << std::tolower(c, loc);
    }
    std::wcout << std::endl;
    return 0;
}

参考:

于 2015-12-23T10:57:13.780 回答