如何将 pugi:char_t* 转换为字符串?
可能的重复:
如何将 pugi::char_t* 转换为字符串
我该如何转换pugi:char_t* 类型转换为 wchar_t 字符串?
我想将 child_value() 的结果与某个 utf8 字符串进行比较,如果没有这种转换,我无法做到这一点。
for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
{
wchar_t* wordValue = it->child("WORDVALUE").child_value();
}
此赋值返回错误,因为右侧是 pugi::char_t* 左侧是 whar_t*
谢谢
Possible Duplicate:
How to convert pugi::char_t* to string
how can I convert pugi:char_t* type to wchar_t string?
I want to compare the result of child_value() to some utf8 string and without that convertion I cant do that.
for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
{
wchar_t* wordValue = it->child("WORDVALUE").child_value();
}
this assignment return error because the right side is pugi::char_t* and the left side is whar_t*
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个想法。粗略的版本涉及修改库,这可能会在将来破坏一些东西,但让我们开始吧:
我们已经有了这两个函数:
我们还知道
pugi::char_t
是char
或wchar_t
。因此,为了在 pugi::char_t 上调用转换函数,我们需要的是一个重载,您可以将其添加到标头中:现在您可以这样写:
这将在两种编译器设置中工作,这要归功于重载,并且如果
pugi::char_t
已经是char
,那么这将被完全优化(当然假设编码已经是 UTF-8,而不是任何其他 8 位编码!)。(您还应该提供采用
std::string
和std::wstring
参数的重载,以支持pugi::string_t
的相同魔力.)如果您担心污染库,请编写自己的包装器:
Here's an idea. The crude version involves modifying the library, which may break stuff in the future, but let's begin with that:
We already have these two functions:
We also know that
pugi::char_t
is eitherchar
orwchar_t
. So all we need in order to call the conversion function onpugi::char_t
is an overload, which you may add to your headers:Now you can write this:
This will work in both compiler settings thanks to the overload, and if
pugi::char_t
is alreadychar
, then this will be optimized out entirely (assuming of course that the encoding was already UTF-8, and not any other 8-bit encoding!).(You should also provide the overloads that take
std::string
andstd::wstring
arguments to support the same magic forpugi::string_t
.)If you worry about polluting the library, write your own wrapper: