gpt4 book ai didi

c++ - 是否可以将引号 („) 放入 Char

转载 作者:行者123 更新时间:2023-11-28 01:21:10 29 4
gpt4 key购买 nike

我正在尝试 push_back()一个 std::vector<char> 的符号.

我一直收到错误:

character too large for enclosing character literal type

void CharVect (std::vector<char>&temp)
{
temp.push_back('-');
temp.push_back(',');
temp.push_back('.');
temp.push_back('?');
temp.push_back('!');
temp.push_back(':');
temp.push_back(';');
temp.push_back('(');
temp.push_back(')');
temp.push_back('„'); // error
temp.push_back('”'); // error, but " works
temp.push_back('{');
temp.push_back('}');
temp.push_back('*');
temp.push_back('#');
}

最佳答案

使用wchar_t:

#include <vector>

void CharVect(std::vector<wchar_t> &temp)
{
temp.push_back(L'-');
temp.push_back(L',');
temp.push_back(L'.');
temp.push_back(L'?');
temp.push_back(L'!');
temp.push_back(L':');
temp.push_back(L';');
temp.push_back(L'(');
temp.push_back(L')');
temp.push_back(L'„'); // works
temp.push_back(L'”'); // works
temp.push_back(L'{');
temp.push_back(L'}');
temp.push_back(L'*');
temp.push_back(L'#');
}

Live Demo

或者您可以考虑使用 std::wstring 来存储宽字符。

作为Gem Taylor points out ,如果您需要处理扩展的表情符号,您可能需要考虑使用 wchar32_t

关于c++ - 是否可以将引号 („) 放入 Char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56225788/

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