【问题标题】:Thousand separator with wide/unicode stream带有宽/unicode流的千位分隔符
【发布时间】:2014-05-25 15:36:56
【问题描述】:

可能是最好的描述方式,我想要实现的是结合以下线程的解决方案: Windows Unicode C++ Stream Output Failure(但使用 utf-16)

How to print a number with a space as thousand separator?(但用点作为分隔符)

以下解决方案尝试对输出没有影响:

//include appropriate headers

struct DotSepUTF16 : public codecvt_utf16<wchar_t, 0x10ffffUL, little_endian>
{
    wchar_t do_thousands_sep()const{ return L'.'; }
    string do_grouping(){ return "\3"; }
};
int main()
{
unsigned long num=135412565UL;
locale dot_separated(locale(), new DotSepUTF16);
wofstream fout(L"C:\\Work\\report.htm");
fout.imbue(dot_separated);
const unsigned short BOM= 0xFEFF;
fout.write((wchar_t*)&BOM, 1);
fout<<num;//still no separation
}

//second attempt

struct DotSep : public numpunct < wchar_t >
{
     wchar_t do_thousands_sep()const{ return L'.'; }
     string do_grouping(){ return "\3"; }
};

int main(void)
{  
unsigned long num=135412565UL;
locale utf16(locale(), new codecvt_utf16<wchar_t, 0x10ffffUL, little_endian>());
locale dot_separated(locale(), new DotSep);
locale mylocale(utf16, dot_separated, locale::numeric);//try to combine the locales for utf16 and dot separation
wofstream fout(L"C:\\Work\\report.htm");
fout.imbue(mylocale);
const unsigned short BOM= 0xFEFF;
fout.write((wchar_t*)&BOM, 1);
fout<<num; //again no dots
}

另外,由于 MVS/windows 已经将 UTF16 用于宽字符串,我想知道为什么需要转换为 utf16。在我看来,这是对 CPU 资源的浪费。没有额外的不必要的转换就不可能写文件吗?以二进制模式编写是可能的,但有点愚蠢,因为它会丧失输出流提供的所有便利

编辑:忘了提到我使用 MVS 2013 和 Intel 编译器

【问题讨论】:

    标签: visual-c++ unicode widestring


    【解决方案1】:

    第二个变种工作,一旦你把do_groupingconst,就像

    string do_grouping() const { return "\3"; }
    

    正如所写,你的重载但不覆盖基类方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多