<legend id='rqDaU'><style id='rqDaU'><dir id='rqDaU'><q id='rqDaU'></q></dir></style></legend>
    <tfoot id='rqDaU'></tfoot>

    1. <i id='rqDaU'><tr id='rqDaU'><dt id='rqDaU'><q id='rqDaU'><span id='rqDaU'><b id='rqDaU'><form id='rqDaU'><ins id='rqDaU'></ins><ul id='rqDaU'></ul><sub id='rqDaU'></sub></form><legend id='rqDaU'></legend><bdo id='rqDaU'><pre id='rqDaU'><center id='rqDaU'></center></pre></bdo></b><th id='rqDaU'></th></span></q></dt></tr></i><div id='rqDaU'><tfoot id='rqDaU'></tfoot><dl id='rqDaU'><fieldset id='rqDaU'></fieldset></dl></div>

    2. <small id='rqDaU'></small><noframes id='rqDaU'>

      • <bdo id='rqDaU'></bdo><ul id='rqDaU'></ul>

      如何将 wstring 转换为字符串?

      How to convert wstring into string?(如何将 wstring 转换为字符串?)
          • <bdo id='lYx1T'></bdo><ul id='lYx1T'></ul>
          • <tfoot id='lYx1T'></tfoot>
              <tbody id='lYx1T'></tbody>

            <legend id='lYx1T'><style id='lYx1T'><dir id='lYx1T'><q id='lYx1T'></q></dir></style></legend>

            <small id='lYx1T'></small><noframes id='lYx1T'>

              • <i id='lYx1T'><tr id='lYx1T'><dt id='lYx1T'><q id='lYx1T'><span id='lYx1T'><b id='lYx1T'><form id='lYx1T'><ins id='lYx1T'></ins><ul id='lYx1T'></ul><sub id='lYx1T'></sub></form><legend id='lYx1T'></legend><bdo id='lYx1T'><pre id='lYx1T'><center id='lYx1T'></center></pre></bdo></b><th id='lYx1T'></th></span></q></dt></tr></i><div id='lYx1T'><tfoot id='lYx1T'></tfoot><dl id='lYx1T'><fieldset id='lYx1T'></fieldset></dl></div>
                本文介绍了如何将 wstring 转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                问题是如何将wstring转成字符串?

                The question is how to convert wstring to string?

                我有下一个例子:

                #include <string>
                #include <iostream>
                
                int main()
                {
                    std::wstring ws = L"Hello";
                    std::string s( ws.begin(), ws.end() );
                
                  //std::cout <<"std::string =     "<<s<<std::endl;
                    std::wcout<<"std::wstring =    "<<ws<<std::endl;
                    std::cout <<"std::string =     "<<s<<std::endl;
                }
                

                带有注释行的输出是:

                std::string =     Hello
                std::wstring =    Hello
                std::string =     Hello
                

                但没有只是:

                std::wstring =    Hello
                

                这个例子有什么问题吗?我可以像上面那样进行转换吗?

                Is anything wrong in the example? Can I do the conversion like above?

                编辑

                新的例子(考虑到一些答案)是

                New example (taking into account some answers) is

                #include <string>
                #include <iostream>
                #include <sstream>
                #include <locale>
                
                int main()
                {
                    setlocale(LC_CTYPE, "");
                
                    const std::wstring ws = L"Hello";
                    const std::string s( ws.begin(), ws.end() );
                
                    std::cout<<"std::string =     "<<s<<std::endl;
                    std::wcout<<"std::wstring =    "<<ws<<std::endl;
                
                    std::stringstream ss;
                    ss << ws.c_str();
                    std::cout<<"std::stringstream =     "<<ss.str()<<std::endl;
                }
                

                输出是:

                std::string =     Hello
                std::wstring =    Hello
                std::stringstream =     0x860283c
                

                因此不能使用字符串流将 wstring 转换为字符串.

                therefore the stringstream can not be used to convert wstring into string.

                推荐答案

                以下是根据其他建议制定的解决方案:

                Here is a worked-out solution based on the other suggestions:

                #include <string>
                #include <iostream>
                #include <clocale>
                #include <locale>
                #include <vector>
                
                int main() {
                  std::setlocale(LC_ALL, "");
                  const std::wstring ws = L"l";
                  const std::locale locale("");
                  typedef std::codecvt<wchar_t, char, std::mbstate_t> converter_type;
                  const converter_type& converter = std::use_facet<converter_type>(locale);
                  std::vector<char> to(ws.length() * converter.max_length());
                  std::mbstate_t state;
                  const wchar_t* from_next;
                  char* to_next;
                  const converter_type::result result = converter.out(state, ws.data(), ws.data() + ws.length(), from_next, &to[0], &to[0] + to.size(), to_next);
                  if (result == converter_type::ok or result == converter_type::noconv) {
                    const std::string s(&to[0], to_next);
                    std::cout <<"std::string =     "<<s<<std::endl;
                  }
                }
                

                这通常适用于 Linux,但会在 Windows 上产生问题.

                This will usually work for Linux, but will create problems on Windows.

                这篇关于如何将 wstring 转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)
                <legend id='O1bsi'><style id='O1bsi'><dir id='O1bsi'><q id='O1bsi'></q></dir></style></legend>

                      <bdo id='O1bsi'></bdo><ul id='O1bsi'></ul>

                        <i id='O1bsi'><tr id='O1bsi'><dt id='O1bsi'><q id='O1bsi'><span id='O1bsi'><b id='O1bsi'><form id='O1bsi'><ins id='O1bsi'></ins><ul id='O1bsi'></ul><sub id='O1bsi'></sub></form><legend id='O1bsi'></legend><bdo id='O1bsi'><pre id='O1bsi'><center id='O1bsi'></center></pre></bdo></b><th id='O1bsi'></th></span></q></dt></tr></i><div id='O1bsi'><tfoot id='O1bsi'></tfoot><dl id='O1bsi'><fieldset id='O1bsi'></fieldset></dl></div>
                          <tbody id='O1bsi'></tbody>
                      • <tfoot id='O1bsi'></tfoot>
                      • <small id='O1bsi'></small><noframes id='O1bsi'>