是否有任何统一的方法可以使C+&#x2B中的控制台输出对齐控制台输出。
我遇到了一项要求,以使控制台中的输出文本对齐。输出文本存储为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论