相当于%02d与std: stringstream?

时间:2021-02-18 15:59:51

I want to output an integer to a std::stringstream with the equivalent format of printf's %02d. Is there an easier way to achieve this than:

我想向std: stringstream输出一个整数,格式与printf的%02d相同。有什么比:

std::stringstream stream;
stream.setfill('0');
stream.setw(2);
stream << value;

Is it possible to stream some sort of format flags to the stringstream, something like (pseudocode):

是否有可能将某种格式标志流到stringstream中,比如(伪代码):

stream << flags("%02d") << value;

4 个解决方案

#1


66  

You can use the standard manipulators from <iomanip> but there isn't a neat one that does both fill and width at once:

你可以使用来自 的标准机械手,但是没有一个整齐的既可以填充又可以同时宽度:

stream << std::setfill('0') << std::setw(2) << value;

It wouldn't be hard to write your own object that when inserted into the stream performed both functions:

当插入到流中时,要编写自己的对象来执行这两个功能并不困难:

stream << myfillandw( '0', 2 ) << value;

E.g.

如。

struct myfillandw
{
    myfillandw( char f, int w )
        : fill(f), width(w) {}

    char fill;
    int width;
};

std::ostream& operator<<( std::ostream& o, const myfillandw& a )
{
    o.fill( a.fill );
    o.width( a.width );
    return o;
}

#2


9  

You can use

您可以使用

stream<<setfill('0')<<setw(2)<<value;

#3


9  

You can't do that much better in standard C++. Alternatively, you can use Boost.Format:

在标准c++中,你不可能做得更好。你也可以使用Boost.Format:

stream << boost::format("%|02|")%value;

#4


1  

Is it possible to stream some sort of format flags to the stringstream?

是否有可能将某种格式标志流到stringstream中?

Unfortunately the standard library doesn't support passing format specifiers as a string, but you can do this with the fmt library:

不幸的是,标准库不支持将格式说明符作为字符串传递,但是可以使用fmt库:

std::string result = fmt::format("{:02}", value); // Python syntax

or

std::string result = fmt::sprintf("%02d", value); // printf syntax

You don't even need to construct std::stringstream. The format function will return a string directly.

甚至不需要构造std: stringstream。format函数将直接返回一个字符串。

Disclaimer: I'm the author of the fmt library.

免责声明:我是fmt图书馆的作者。

#1


66  

You can use the standard manipulators from <iomanip> but there isn't a neat one that does both fill and width at once:

你可以使用来自 的标准机械手,但是没有一个整齐的既可以填充又可以同时宽度:

stream << std::setfill('0') << std::setw(2) << value;

It wouldn't be hard to write your own object that when inserted into the stream performed both functions:

当插入到流中时,要编写自己的对象来执行这两个功能并不困难:

stream << myfillandw( '0', 2 ) << value;

E.g.

如。

struct myfillandw
{
    myfillandw( char f, int w )
        : fill(f), width(w) {}

    char fill;
    int width;
};

std::ostream& operator<<( std::ostream& o, const myfillandw& a )
{
    o.fill( a.fill );
    o.width( a.width );
    return o;
}

#2


9  

You can use

您可以使用

stream<<setfill('0')<<setw(2)<<value;

#3


9  

You can't do that much better in standard C++. Alternatively, you can use Boost.Format:

在标准c++中,你不可能做得更好。你也可以使用Boost.Format:

stream << boost::format("%|02|")%value;

#4


1  

Is it possible to stream some sort of format flags to the stringstream?

是否有可能将某种格式标志流到stringstream中?

Unfortunately the standard library doesn't support passing format specifiers as a string, but you can do this with the fmt library:

不幸的是,标准库不支持将格式说明符作为字符串传递,但是可以使用fmt库:

std::string result = fmt::format("{:02}", value); // Python syntax

or

std::string result = fmt::sprintf("%02d", value); // printf syntax

You don't even need to construct std::stringstream. The format function will return a string directly.

甚至不需要构造std: stringstream。format函数将直接返回一个字符串。

Disclaimer: I'm the author of the fmt library.

免责声明:我是fmt图书馆的作者。