简体   繁体   English

如何解释 C++ 字符串中的 \\u 转义码?

[英]How do I interpret a \u escape code in a C++ string?

I want to build a list of unicode glyphs programatically in C++, but I can't find a way to get around the compiler error \\u used with no following hex digits我想在 C++ 中以编程方式构建一个 unicode 字形列表,但我找不到解决编译器错误\\u used with no following hex digits

#include <iostream>
#include <sstream>

using namespace std;

int main() 
{
    int unicode = 0x2200;
    stringstream result;
    for (int i = 0; i<0x0F; ++i)
    {
    unicode += i ;
    result << "\u" << hex << unicode << "\n";
    };

    cout << result.str();

    return 0;
}```



It is used to indicate Universal Character name.用于表示通用字符名称。 From this source we can see a more detailed description:这个来源我们可以看到更详细的描述:

In character literals and native (non-raw) string literals, any character may be represented by a universal character name.在字符文字和本机(非原始)字符串文字中,任何字符都可以由通用字符名称表示。 Universal character names are formed by a prefix \\U followed by an eight-digit Unicode code point, or by a prefix \\u followed by a four digit Unicode code point.通用字符名称由前缀 \\U 后跟八位 Unicode 代码点或前缀 \\u 后跟四位 Unicode 代码点组成。 All eight or four digits, respectively, must be present to make a well-formed universal character name.必须分别存在所有八位或四位数字才能形成格式良好的通用字符名称。

The error that you get is likely due to the absence of a variable value.您得到的错误可能是由于缺少变量值。 I do not see where the hex is declared in provided context.我没有看到在提供的上下文中在哪里声明了hex

Thanks for the answers.感谢您的回答。

I did find a way to increment one glyph at a time ( I am referring to this list https://unicode-table.com/en/#arrows )我确实找到了一次增加一个字形的方法(我指的是这个列表https://unicode-table.com/en/#arrows

#include <iostream>
#include <sstream>

using namespace std;

int main() 
{
    std::string data = "\u2200";
    data[2] += 1;

    cout << "Starting glyph 2200-> \u2200 \n" 
    << "Expected glyph 2201-> \u2201 \n"
    << "Resulting glyph is-> " << data << '\n';

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM