I need a contai开发者_JAVA技巧ner in which I can check if a sequence of elements are present or not. Same thing as substring matching, just for generic collections. I know it's not hard to write, but if it's implemented in some lib already, I wouldn't bother (maybe Boost has something like this?)
Any sequence container will do. You just need to use std::search algorithm to do the search of the sublist:
vector<int> sequence = ...;
vecter<int> sublist = ...;
vector<int>::iterator pos = std::search(
sequence.begin(), sequence.end(),
sublist.begin(), sublist.end());
if(pos == sequence.end())
// not fount
else
// found at pos
Do you want std::search
?
The mismatch
algorithm from STL is pretty much strcmp
for generic containers.
http://www.cplusplus.com/reference/algorithm/mismatch/
精彩评论