如何将ostream转换成字符串

How to convert ostream into a string

在一个函数中,我正在传递 ostream 并想转换成一个字符串。

void func (ostream& stream) {
    /* Needs to convert stream into string */
}
void func (ostream& stream) {
    /* Needs to convert stream into string */
    std::stringstream ss;
    ss << stream.rdbuf();
    std::string myString = ss.str();
}