如何将pugi :: char_t *转换为字符串 [英] How to convert pugi::char_t* to string

查看:850
本文介绍了如何将pugi :: char_t *转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好
我使用pugixml来处理xml文件。我使用这个结构遍历节点

Hi I'm using pugixml to process xml documents. I iterate through nodes using this construction

 pugi::xml_node tools = doc.child("settings");

    //[code_traverse_iter
    for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
    {
        //std::cout << "Tool:";
        cout <<it->name();

    }

问题是it-> name()返回pugi :: char_t *,我需要将其转换为std :: string。是否可以 ??我在pugixml网站上找不到任何信息

the problem is that it->name() returns pugi::char_t* and I need to convert it into std::string. Is it possible ?? I can't find any information on pugixml website

推荐答案

根据手册 pugi :: char_t char wchar_t ,具体取决于您的库配置。这是为了可以在单字节(ASCII或UTF-8)和双字节(通常为UTF-16/32)之间切换。

According to the manual, pugi::char_t is either char or wchar_t, depending on your library configuration. This is so that you can switch between single bytes (ASCII or UTF-8) and double bytes (usually UTF-16/32).

需要改变它到任何东西。但是,如果您使用 wchar_t * 变体,则必须使用匹配的流对象:

This means you don't need to change it to anything. However, if you're using the wchar_t* variant, you will have to use the matching stream object:

#ifdef PUGIXML_WCHAR_MODE
std::wcout << it->name();
#else
std::cout << it->name();
#endif

因为你问,构造一个 std :: string std :: wstring

And, since you asked, to construct a std::string or std::wstring from it:

#ifdef PUGIXML_WCHAR_MODE
std::wstring str = it->name();
#else
std::string str = it->name();
#endif

或者,总是 std :: string 这很少是你想要的!):

#ifdef PUGIXML_WCHAR_MODE
std::string str = as_utf8(it->name());
#else
std::string str = it->name();
#endif

希望这有帮助。

资料来源:粗略浏览pugixml文档。

Source: A cursory glance at the "pugixml" documentation.

这篇关于如何将pugi :: char_t *转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆