cout << setw doesn't align correctly with åäö

牧云@^-^@ 提交于 2019-12-08 15:41:29

问题


The following code reproduces my problem:

#include <iostream>
#include <iomanip>
#include <string>

void p(std::string s, int w)
{
   std::cout << std::left << std::setw(w) << s;
}

int main(int argc, char const *argv[])
{
   p("COL_A", 7);
   p("COL_B", 7);
   p("COL_C", 5);
   std::cout << std::endl;
   p("ABC", 7);
   p("ÅÄÖ", 7);
   p("ABC", 5);
   std::cout << std::endl;
   return 0;
}

This produces the following output:

COL_A  COL_B  COL_C
ABC    ÅÄÖ ABC

If i change "ÅÄÖ" in the code to e.g. "ABC", then it works:

COL_A  COL_B  COL_C
ABC    ABC    ABC  

Why is this happening?


回答1:


Along with imbuing std::wcout with the proper locale, you probably have to switch to wide strings as well. For example:

void p(std::wstring s, int w)
{
   std::wcout << std::left << std::setw(w) << s;
}

int main(int argc, char const *argv[])
{
   setlocale(LC_ALL, "en_US.utf8");
   std::locale loc("en_US.UTF-8");
   std::wcout.imbue(loc);

   p(L"COL_A", 7);
   p(L"COL_B", 7);
   p(L"COL_C", 5);
   std::wcout << std::endl;
   p(L"ABC", 7);
   p(L"ÅÄÖ", 7);
   p(L"ABC", 5);
   std::wcout << std::endl;
   return 0;
}

Demo




回答2:


It happens because those characters (Ä, Ö, ...) are unicode character that are likely encoded in UTF-8. This means that each character takes up several bytes (two in your case, up to four in the general case). setw OTOH does not know about UTF-8 - it just counts and thus aligns bytes.




回答3:


The problem is that your source code is certainly stored in UTF8, meaning 2 bytes for each letter of ÅÄÖ, and the locale for cout isn't set accordingly.

Hence your cout thinks it outputs 3x2=6 chars, and adds only one space to reach the 7 expected. Change the locale using imbue() to set it to UTF8.




回答4:


This works for both accented latin letters and CJK characters:

#include <iomanip>
#include <iostream>
#include <string>
#include <wchar.h>

typedef decltype(std::setw(0)) setw_type;

setw_type
setww(int w, std::wstring s)
{
  auto delta = wcswidth(s.c_str(), s.length()) - s.length();
  return std::setw(w - delta);
}

void
print_wstring(std::wstring s, int w)
{
  std::wcout << setww(w, s) << s;
}

int
main(int argc, char * argv[])
{
  auto locale_string = "zh_CN.utf8";
  setlocale(LC_ALL, locale_string);
  std::locale loc(locale_string);
  std::wcout.imbue(loc);
  print_wstring(L"|一二三四", 9);
  print_wstring(L"|一二三四", 9);
  std::wcout << std::endl;
  print_wstring(L"公道", 9);
  print_wstring(L"自在人心", 9);
  std::wcout << std::endl;
}

Result:

g++ test01.cpp -o test01.exe  && ./test01.exe
|一二三四|一二三四
    公道 自在人心


来源:https://stackoverflow.com/questions/29188948/cout-setw-doesnt-align-correctly-with-%c3%a5%c3%a4%c3%b6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!