我在C ++中有一个wstring变量和一个字符串变量。我想将它们连接起来,但只是将它们一起添加就会产生构建错误。我怎样才能将它们结合起来?如果我需要将wstring变量转换为字符串,我将如何完成此操作?
//A WCHAR array is created by obtaining the directory of a folder - This is part of my C++ project
WCHAR path[MAX_PATH + 1] = { 0 };
GetModuleFileNameW(NULL, path, MAX_PATH);
PathCchRemoveFileSpec(path, MAX_PATH);
//The resulting array is converted to a wstring
std::wstring wStr(path);
//An ordinary string is created
std::string str = "Test";
//The variables are put into this equation for concatenation - It produces a build error
std::string result = wStr + str;
答案 0 :(得分:2)
首先将wstring
转换为string
,例如this:
std::string result = std::string(wStr.begin(), wStr.end()) + str;
或wStr
是否包含非ASCII字符:
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::string wStrAsStr = converter.to_bytes(wStr);
std::string result = wStrAsStr + str;