来源:
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