std::setw and unicode character

孤街醉人 提交于 2019-12-23 19:33:49

问题


My problem is shown in the following minimal example:

#include <iostream>
#include <string>
#include <iomanip>
int main()
{
    int width = 15;
    std::cout << std::left;
    std::cout << std::setw(width) << "Prints well" << std::setw(width) << "This too" << '\n';
    std::cout << std::setw(width) << "\u221E" << std::setw(width) << "This not?" << '\n';
    std::cout << std::setw(width+2) << "\u221E" << std::setw(width) << "This is good" << '\n';
}

Compiled using g++, it prints:

Prints well    This too       
∞            This not?      
∞              This is good

So it seems that the unicode symbol uses 3 spaces from the setw instead of one. Is there a simple way to fix this, not knowing beforehand whether a unicode character will be in the string?

来源:https://stackoverflow.com/questions/49858446/stdsetw-and-unicode-character

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