在 C++20 中,可以使用 Unicode 字符集来判断字符串中是否包含中文字符。以下是一个示例代码:

#include <iostream>
#include <string>
#include <ranges>

int main()
{
    std::wstring str = L"Hello, 世界!";
    auto is_chinese = [](wchar_t c) {
        return std::ranges::contains(std::wview(L"\u4E00"sv, L"\u9FFF"sv), c);
    };
    bool has_chinese = std::ranges::any_of(str, is_chinese);
    std::cout << std::boolalpha << has_chinese << '\n';
}

在这个示例中,我们使用了 C++20 中的 std::ranges::containsstd::ranges::any_of 算法来判断字符串中是否包含中文字符。std::ranges::contains 用于判断一个字符是否在某个字符范围内,std::ranges::any_of 则用于判断字符串中是否有满足某个条件的字符。在 is_chinese 函数中,我们将中文字符的范围设为 Unicode 中的 \u4E00\u9FFF。如果字符串中包含任意一个中文字符,has_chinese 就会被设置为 true

c++20 判断字符串是不是中文

原文地址: https://www.cveoy.top/t/topic/sRC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录