跳转至

cpp宽字符wchar

std::string基于char,8bit。std::wstring基于wchar_t,Linux上4字节,Windows上2字节。在Windows上一般要用std::wstring而非std::string

// wenet/runtime/core/utils/string.cc
#ifdef _MSC_VER
std::wstring ToWString(const std::string& str) {
  unsigned len = str.size() * 2;
  setlocale(LC_CTYPE, "");
  wchar_t* p = new wchar_t[len];
  mbstowcs(p, str.c_str(), len);
  std::wstring wstr(p);
  delete[] p;
  return wstr;
}
#endif

c++ - std::wstring VS std::string - Stack Overflow


最后更新: 2022-06-08

评论