Is there any uniform way to align console output in C++

Viewed 65

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.

0 Answers
Related