gpt4 book ai didi

c++ - unicode 字符的相等性

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:47 25 4
gpt4 key购买 nike

我需要写一个简单的比较。

if (user entered "Д" first)
{
//do something
}

问题是我需要比较 Unicode 字符(在本例中为俄语字母“Д”)。

我设法通过以下方式做到这一点:

std::string option;
getline(std::cin, option);
if (option.compare(0, 1, u8"Д"))
{
//do something
}

如果不将 std::stringcompare 一起使用,我如何使用 char 执行此操作?如果您为 std::string 提供更好的解决方案,我会很高兴。

最佳答案

远非理想,但它简单且有效(我希望):

#include <iostream>
#include <string>

char const yes[] = u8"Д";
char const no[] = u8"Н";

int main()
{
std::string str;
while (std::cin >> str)
{

std::cout << "Let's Да? " << str << "! => " << std::boolalpha << (str.substr(0, sizeof(yes) - 1) == yes) << std::endl;
std::cout << "Let's Нет? " << str << "! => " << std::boolalpha << (str.substr(0, sizeof(no) - 1) == no) << std::endl;
}
}

演示:https://ideone.com/Vhtl1T

Let's Да? Да! => true
Let's Нет? Да! => false
Let's Да? Нет! => false
Let's Нет? Нет! => true
Let's Да? Нет! => false
Let's Нет? Нет! => true
Let's Да? Да! => true
Let's Нет? Да! => false

关于c++ - unicode 字符的相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50555942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com