是否有任何统一的方法可以使C+&#x2B中的控制台输出对齐控制台输出。

发布于 2025-01-22 04:05:35 字数 724 浏览 4 评论 0原文

我遇到了一项要求,以使控制台中的输出文本对齐。输出文本存储为std :: Wstring,并编码为utf-8。使此任务棘手的事情是,输出文本包含ASCII字符和日语字符,例如5回1ション1ション4AスF。使用setW()wprintf(l“%-10S”)由于ASCII字符和日语字符的宽度不同,因此无法对其进行对齐。
例如,

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    std::locale::global(std::locale(""));
    wstring s[] = {L"短3マ231ー○",L"のき3ーナ",L"ぐマ",L"ろにト"};
    for(int i=0;i<4;i++)    wcout << setw(10) << s[i] <<123<< endl;
}

将是:

    短3マ231ー○123
     のき3ーナ123
       ぐマ123
      ろにト123

但是,如果文本仅包含ASCII字符,则可以很好地工作。

我知道我只能编写一个新功能来自行对齐,但是我想知道是否已经有一个可靠的解决方案。

I met a requirement to align the output text in the console. The output texts are stored as std::wstring and encoded as UTF-8. The thing that makes this task tricky is, the output text contains both ASCII characters and Japanese characters, e.g. ナ5回1ション4aスF. Use setw() or wprintf(L"%-10s") directly can't align it due to the different widths of ASCII characters and Japanese characters.
For example,

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    std::locale::global(std::locale(""));
    wstring s[] = {L"短3マ231ー○",L"のき3ーナ",L"ぐマ",L"ろにト"};
    for(int i=0;i<4;i++)    wcout << setw(10) << s[i] <<123<< endl;
}

will be like:

    短3マ231ー○123
     のき3ーナ123
       ぐマ123
      ろにト123

But it works perfectly if the text only contain ASCII characters.

I know I can just write a new function for aligning it on my own, but I'm wondering if there already have a reliable solution for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文