Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

c++11 - Have `std::ostreambuf_iterator` write to a string instead of `std::cout` in this example

I am going through an example of std::regex_replace. The example uses this

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

std::ostreambuf_iterator<char>(std::cout) writes to std::cout how do I make it write to a string instead ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use stringstream:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...