相关疑难解决方法(0)

std :: wstring VS std :: string

我无法理解之间的差异std::stringstd::wstring.我知道wstring支持Unicode字符等宽字符.我有以下问题:

  1. 我什么时候应该std::wstring用完std::string
  2. 可以std::string保存整个ASCII字符集,包括特殊字符吗?
  3. std::wstring由所有流行的C++编译器的支持?
  4. 什么是" 广角 "?

c++ string unicode c++-faq wstring

716
推荐指数
7
解决办法
29万
查看次数

在Windows控制台中正确打印utf8字符

这是我尝试这样做的方式:

#include <stdio.h>
#include <windows.h>
using namespace std;

int main() {
  SetConsoleOutputCP(CP_UTF8);
   //german chars won't appear
  char const* text = "aäbcdefghijklmnoöpqrsßtuüvwxyz";
  int len = MultiByteToWideChar(CP_UTF8, 0, text, -1, 0, 0);
  wchar_t *unicode_text = new wchar_t[len];
  MultiByteToWideChar(CP_UTF8, 0, text, -1, unicode_text, len);
  wprintf(L"%s", unicode_text);
}
Run Code Online (Sandbox Code Playgroud)

效果是只显示我们的ascii字符.没有显示错误.源文件以utf8编码.

那么,我在这里做错了什么?

到WouterH:

int main() {
  SetConsoleOutputCP(CP_UTF8);
  const wchar_t *unicode_text = L"aäbcdefghijklmnoöpqrsßtuüvwxyz";
  wprintf(L"%s", unicode_text);
}
Run Code Online (Sandbox Code Playgroud)
  • 这也行不通.效果是一样的.我的字体当然是Lucida Console.

第三步:

#include <stdio.h>
#define _WIN32_WINNT 0x05010300
#include <windows.h>
#define _O_U16TEXT  0x20000
#include <fcntl.h>

using namespace std;

int main() { …
Run Code Online (Sandbox Code Playgroud)

c++ console mingw utf-8 windows-xp-sp3

16
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×2

c++-faq ×1

console ×1

mingw ×1

string ×1

unicode ×1

utf-8 ×1

windows-xp-sp3 ×1

wstring ×1