tags:

views:

90

answers:

3

I need a container 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?)

+2  A: 

Do you want std::search?

Oli Charlesworth
+8  A: 

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
ybungalobill
oh god how did I miss this one :). Thanks.
Tamás Szelei
@Tamás: If you think this is the answer to your question, please vote it up ans ACCEPT it by clicking the v-like thing under the answer vote count. Thank you
Armen Tsirunyan
@Armen, yeah, it just wouldn't let me to accept it that fast, so I had to wait.
Tamás Szelei
A: 

The mismatch algorithm from STL is pretty much strcmp for generic containers.

http://www.cplusplus.com/reference/algorithm/mismatch/

Let_Me_Be