cout << setw 没有与 åäö 正确对齐
cout << setw doesn't align correctly with åäö
以下代码重现了我的问题:
#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;
}
这会产生以下输出:
COL_A COL_B COL_C
ABC ÅÄÖ ABC
如果我将代码中的“ÅÄÖ”更改为例如"ABC",然后就可以了:
COL_A COL_B COL_C
ABC ABC ABC
为什么会这样?
问题是您的源代码肯定是以 UTF8 格式存储的,这意味着 ÅÄÖ 的每个字母占 2 个字节,并且 cout 的语言环境没有相应设置。
因此您的 cout 认为它输出 3x2=6 个字符,并且只添加一个 space 以达到预期的 7 个。使用 imbue() 更改语言环境以将其设置为 UTF8。
发生这种情况是因为这些字符(Ä、Ö、...)是可能以 UTF-8 编码的 unicode 字符。这意味着每个字符占用几个字节(在您的情况下为两个,在一般情况下最多为四个)。 setw
OTOH 不知道 UTF-8 - 它只是计数并因此对齐字节。
除了为 std::wcout
注入正确的语言环境外,您可能还必须切换到宽字符串。例如:
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;
}
这对带重音的拉丁字母和 CJK 字符都有效:
#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;
}
结果:
g++ test01.cpp -o test01.exe && ./test01.exe
|一二三四|一二三四
公道 自在人心
C++20 std::format
将正确处理此问题
std::cout << std::format("{:7}{:7}{:5}\n", "COL_A", "COL_B", "COL_C");
std::cout << std::format("{:7}{:7}{:5}\n", "ABC", "ÅÄÖ", "ABC");
输出:
COL_A COL_B COL_C
ABC ÅÄÖ ABC
在此期间您可以使用 the {fmt} library, std::format
is based on. {fmt} also provides the print
function that makes this even easier and more efficient (godbolt):
#include <fmt/core.h>
int main() {
fmt::print("{:7}{:7}{:5}\n", "COL_A", "COL_B", "COL_C");
fmt::print("{:7}{:7}{:5}\n", "ABC", "ÅÄÖ", "ABC");
}
免责声明:我是 {fmt} 和 C++20 的作者 std::format
。
以下代码重现了我的问题:
#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;
}
这会产生以下输出:
COL_A COL_B COL_C
ABC ÅÄÖ ABC
如果我将代码中的“ÅÄÖ”更改为例如"ABC",然后就可以了:
COL_A COL_B COL_C
ABC ABC ABC
为什么会这样?
问题是您的源代码肯定是以 UTF8 格式存储的,这意味着 ÅÄÖ 的每个字母占 2 个字节,并且 cout 的语言环境没有相应设置。
因此您的 cout 认为它输出 3x2=6 个字符,并且只添加一个 space 以达到预期的 7 个。使用 imbue() 更改语言环境以将其设置为 UTF8。
发生这种情况是因为这些字符(Ä、Ö、...)是可能以 UTF-8 编码的 unicode 字符。这意味着每个字符占用几个字节(在您的情况下为两个,在一般情况下最多为四个)。 setw
OTOH 不知道 UTF-8 - 它只是计数并因此对齐字节。
除了为 std::wcout
注入正确的语言环境外,您可能还必须切换到宽字符串。例如:
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;
}
这对带重音的拉丁字母和 CJK 字符都有效:
#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;
}
结果:
g++ test01.cpp -o test01.exe && ./test01.exe
|一二三四|一二三四
公道 自在人心
C++20 std::format
将正确处理此问题
std::cout << std::format("{:7}{:7}{:5}\n", "COL_A", "COL_B", "COL_C");
std::cout << std::format("{:7}{:7}{:5}\n", "ABC", "ÅÄÖ", "ABC");
输出:
COL_A COL_B COL_C
ABC ÅÄÖ ABC
在此期间您可以使用 the {fmt} library, std::format
is based on. {fmt} also provides the print
function that makes this even easier and more efficient (godbolt):
#include <fmt/core.h>
int main() {
fmt::print("{:7}{:7}{:5}\n", "COL_A", "COL_B", "COL_C");
fmt::print("{:7}{:7}{:5}\n", "ABC", "ÅÄÖ", "ABC");
}
免责声明:我是 {fmt} 和 C++20 的作者 std::format
。