首页 > 解决方案 > 在此示例中,让 `std::ostreambuf_iterator` 写入字符串而不是 `std::cout`

问题描述

我正在通过一个例子std::regex_replace。该示例使用此

std::regex_replace(std::ostreambuf_iterator<char>(std::cout),source.begin(), source.end(), search, "");

std::ostreambuf_iterator<char>(std::cout)写到std::cout我如何让它写到一个字符串呢?

标签: c++11

解决方案


使用字符串流:

std::stringstream str;
std::regex_replace(std::ostreambuf_iterator<char>(str),source.begin(), source.end(), search, "");
// str.str() is a string with the result

推荐阅读