stack user stack user - 1 year ago 139
C++ Question

can I std::find a string in a stringstream?

I have a stringstream that I'd like to iterate and determine if a substring exists in it.

I know that I could just convert to a string and do std::string::find(), but I was just hoping to avoid the conversion from stringstream to string if possible.

I understand the following won't work because the istream_iterator uses char as its type (not string)

stringstream ssBody;
string sFindThis;


auto itr = std::find (

But can I somehow search for a string in stringstream with std::find or similar without a conversion to string?

Answer Source

The C++ standard does not define any std::[io]?stringstream methods for searching its contents.

Neither can you use std::istreambuf_iterator together with std::search(), since std::istreambuf_iterator is an input iterator, but std::search() requires a forward iterator.

The only effective way to search a string stream is to convert it to a std::string, first.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download