C++如何检查字母isalpha(不是拉丁字母)

2022-01-07 00:00:00 string character-encoding c++

所以我需要看看我的角色是不是一个字母.我尝试使用 isalpha() 函数,但是,如果我尝试传递非拉丁字母(例如 ?, ?, ?, ?, ?, ?, ?, ū, ?)我得到一个错误,这似乎表明 isalpha函数只接受 ASCII 码中介于 0 和 255 之间的字符.有什么办法可以解决这个问题?

so I need to see if my character is a letter. I tried using isalpha() function, however, if I try to pass not latin letter (for example ?, ?, ?, ?, ?, ?, ?, ū, ?) I get an error, that seems to state that isalpha function accepts only chars that in ASCII code are between 0 and 255. Is there any way to overcome this problem?

推荐答案

您可以使用 locale 版本的 std::isalpha.以链接参考中的示例为例:

You can use a locale version of std::isalpha. Taking an example from the linked reference:

#include <iostream>
#include <locale>
int main()
{
    const wchar_t c = L'u042f'; // cyrillic capital letter ya

    std::locale loc1("C");
    std::cout << "isalpha('Я?', C locale) returned "
               << std::boolalpha << std::isalpha(c, loc1) << '
';

    std::locale loc2("en_US.UTF8");
    std::cout << "isalpha('Я', Unicode locale) returned "
              << std::boolalpha << std::isalpha(c, loc2) << '
';
}

输出:

isalpha('Я?', C locale) returned false
isalpha('Я', Unicode locale) returned true

相关文章