Reputation: 1366
I have a substring defined by two iterators (start
and end
). I need to check if this substring is present in another string.
Is there a standard library algorithm or string member I can use or adapt to do this without creating a whole new string object (std::string(start, end)
) just for this purpose?
e.g.
struct Substring
{
std::string::const_iterator start, end;
};
auto found = std::contains(whole.begin(), whole.end(), substring.start, substring.end); // ???
Upvotes: 0
Views: 1100
Reputation: 154876
You can use the std::string::find
method:
auto found = (whole.find(&*substring.start, 0, substring.end - substring.start)
!= std::string::npos);
The advantage over std::search
is that std::find
works on strings and can be implemented using Boyer-Moore. Sadly, that is not how gcc's libstdc++ implements it.
Upvotes: 0
Reputation: 55395
bool found =
std::search(hay.begin(), hay.end(), needle.begin(), needle.end()) != hay.end();
Upvotes: 11