如何用 chrono::milliseconds 连接一个字符串?

How to concatenate a string with chrono::milliseconds?

我需要一个字符串,其中包含以毫秒为单位的时间戳。 我以这种方式获得了毫秒数(在 Whosebug 上查找之后):

milliseconds ms = duration_cast< milliseconds >(
    system_clock::now().time_since_epoch()
);

现在我必须像这样连接它:

string = "something " + ms + " something else";

有什么帮助吗? 提前谢谢你:)

使用count方法和std::to_string。示例:

string = "something " + std::to_string(ms.count()) + " something else"

您需要一种将 ms 转换为字符串的方法。标准有std::to_string() but that wont work directly with a duration. To convert the duration to a integral type that to_string() can use you need to use the count()函数

string = "something " + std::to_string(ms.count()) + " something else";