How to convert string to wstring?

How to convert string to wstring? - Codejie's C++ Space - C++博客

    How to convert string to wstring?
         来源:http://www.codeguru.com/forum/archive/index.php/t-193852.html

    The copy() function does not automatically make room for the destination, so you must make sure that you have enough room in the wstring.

    Why not just write a function to do the conversion?

    #include <string>
    #include <algorithm>

    // Prototype for conversion functions
    std::wstring StringToWString(const std::string& s);
    std::string WStringToString(const std::wstring& s);

    std::wstring StringToWString(const std::string& s)
    {
    std::wstring temp(s.length(),L' ');
    std::copy(s.begin(), s.end(), temp.begin());
    return temp;
    }


    std::string WStringToString(const std::wstring& s)
    {
    std::string temp(s.length(), ' ');
    std::copy(s.begin(), s.end(), temp.begin());
    return temp;
    }

    using namespace std;

    int main()
    {
    string s1 = "Hello";
    wstring s2 = StringToWString(s1);
    s1 = WStringToString(s2);
    return 0;
    }

    Regards,

    Paul McKenzie

    posted on 2009-03-27 12:35 codejie 阅读(655) 评论(6)  编辑 收藏 引用 所属分类: Resource
    评论
    # re: How to convert string to wstring? 2009-03-27 13:03 陈梓瀚(vczh)

    你试试汉字。  回复  更多评论  
    # re: How to convert string to wstring? 2009-03-27 17:56 codejie

    么问题。  回复  更多评论  
    # re: How to convert string to wstring? 2009-03-27 22:59 陈梓瀚(vczh)

    你如何判断他没事  回复  更多评论  
    # re: How to convert string to wstring?[未登录] 2009-03-29 23:03 codejie

    可以正常读取一个中文文件名的MP3文件。你有什么疑问吗?  回复  更多评论  
    # re: How to convert string to wstring?[未登录] 2013-11-26 01:19 烟圈

    int main()
    {
    //string s1 = "";
    //wstring s2 = StringToWString(s1);
    string s1 = WStringToString(L"中国");

    cout <<s1 <<endl;
    return 0;
    }

    试试肯定不行。。
原文地址:https://www.cnblogs.com/lexus/p/3651483.html