小编dow*_*ash的帖子

登录错误的位置std :: ostringstream?

我使用std :: ostringstream将double格式化为具有特定格式的字符串(使用撇号作为千位分隔符).但是,在某些情况下,ostringstream给了我与我的预期不同的结果.

据我所知,下面代码的预期输出应为"+01"; 相反,它输出"0 + 1".我在这里做错了什么,我怎样才能得到我需要的结果?

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

int main() 
{
    std::ostringstream stream;
    stream << std::showpos; // Always show sign
    stream << std::setw(3); // Minimum 3 characters
    stream << std::setfill( '0' ); // Zero-padded
    stream << 1; // Expected output: "+01"

    std::cout << stream.str(); // Output: "0+1"
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

关于ideone的代码

c++

27
推荐指数
3
解决办法
1300
查看次数

标签 统计

c++ ×1