【问题标题】:What is the default `fill character` of std::stringstream?std::stringstream 的默认“填充字符”是什么?
【发布时间】:2013-12-27 01:17:32
【问题描述】:

是实现定义还是标准建议流的默认填充字符?

示例代码:

#include <iostream>
#include <iomanip>
#include <sstream>

int main ()
{
    std::stringstream stream;
    stream << std::setw( 10 ) << 25 << std::endl;

    std::cout << stream.str() << std::endl;
}

clang++ --stdlib=libstdc++

$ clang++ --stdlib=libstdc++ test.cpp
$ ./a.out | hexdump
0000000 20 20 20 20 20 20 20 20 32 35 0a 0a
000000c
$

clang++ --stdlib=libc++

$ clang++ --stdlib=libc++ test.cpp
$ ./a.out | hexdump
0000000 ff ff ff ff ff ff ff ff 32 35 0a 0a
000000c

版本

$ clang++ --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

我可以使用 std::setfill(' ') 修复它,但我很想知道它是否是 clang 错误。

【问题讨论】:

  • Fedora 19 上没有“libc++”。你的是什么?
  • @rickhg12hs,我在 Mac 上。不确定linux。

标签: c++ stringstream clang++ libc++ setw


【解决方案1】:

根据 27.5.5.2 [basic.ios.cons] 第 3 段/表 128,s 流的默认填充字符是 s.widen(' ')。但是,s.widen(' ') 产生的字符取决于 @ 987654326@ as s.widen(c) 是(27.5.5.3 [basic.ios.members] 第 12 段):

std::use_facet<std::ctype<cT>>(s.getloc()).widen(c)

顺便说一句,当您只写入流并且永远存在no use of std::endl 时,您应该使用std::ostringstream

【讨论】:

  • 谢谢。所以它看起来像一个实现的错误。 s.widen(' ')libstdc++libc++ 返回 0x20,这不是 libc++ 的填充字符。
  • 看起来已经有一个bug filed 并且已修复。
猜你喜欢
  • 1970-01-01
  • 2020-11-08
  • 2018-12-03
  • 2020-05-06
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
  • 2011-01-04
相关资源
最近更新 更多