簡體   English   中英

為什么我不能從 char* 構造 wstring

[英]Why I can't construct a wstring from char*

根據我從這段代碼構建填充一個char*

char* pathAppData = nullptr;
size_t sz = 0;
_dupenv_s(&pathAppData, &sz, "APPDATA");

我可以很容易地用這個代碼和append這個string構造一個string

std::string sPathAppData(pathAppData);
sPathAppData.append("\\MyApplication");

但我無法從此代碼創建wstring 為什么使用wstring這么復雜? 我對所有這些類型都快瘋了。

std::wstring wPathAppData(pathAppData); // Impossible

您必須使用迭代器進行復制。 這就是你可以做到的。

char* pathAppData = nullptr;
size_t sz = 0;
_dupenv_s(&pathAppData, &sz, "APPDATA");
std::string sPathAppData(pathAppData);
sPathAppData.append("\\MyApplication");

std::wstring wPathAppData(begin(sPathAppData), end(sPathAppData));
wcout << wPathAppData << endl;

您可以直接使用wchar_t並使用相應的wchar_t支持的 API 直接將數據檢索到wchar_t然后構造wstring _dupenv_s function 有一個廣泛的對應物 - _wdupenv_s

您的代碼將如下所示:

wchar_t* pathAppData = nullptr;
size_t sz = 0;
_wdupenv_s(&pathAppData, &sz, L"APPDATA");
std::wstring wPathAppData(pathAppData);
wPathAppData.append(L"\\MyApplication")

這也可能是一個有趣的閱讀: std::wstring VS std::string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM